mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add callbacks
This commit is contained in:
parent
5f73fa84ff
commit
0f140fe158
3 changed files with 60 additions and 2 deletions
|
@ -1,7 +1,8 @@
|
||||||
require "active_support/rescuable"
|
require "active_support/rescuable"
|
||||||
|
require "action_mailroom/mailbox/callbacks"
|
||||||
|
|
||||||
class ActionMailroom::Mailbox
|
class ActionMailroom::Mailbox
|
||||||
include ActiveSupport::Rescuable
|
include ActiveSupport::Rescuable, Callbacks
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def receive(inbound_email)
|
def receive(inbound_email)
|
||||||
|
@ -22,7 +23,11 @@ class ActionMailroom::Mailbox
|
||||||
|
|
||||||
def perform_processing
|
def perform_processing
|
||||||
inbound_email.processing!
|
inbound_email.processing!
|
||||||
process
|
|
||||||
|
run_callbacks :process do
|
||||||
|
process
|
||||||
|
end
|
||||||
|
|
||||||
inbound_email.delivered!
|
inbound_email.delivered!
|
||||||
rescue => exception
|
rescue => exception
|
||||||
inbound_email.failed!
|
inbound_email.failed!
|
||||||
|
|
28
lib/action_mailroom/mailbox/callbacks.rb
Normal file
28
lib/action_mailroom/mailbox/callbacks.rb
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
require "active_support/callbacks"
|
||||||
|
|
||||||
|
module ActionMailroom
|
||||||
|
class Mailbox
|
||||||
|
module Callbacks
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
include ActiveSupport::Callbacks
|
||||||
|
|
||||||
|
included do
|
||||||
|
define_callbacks :process
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def before_processing(*methods, &block)
|
||||||
|
set_callback(:process, :before, *methods, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def after_processing(*methods, &block)
|
||||||
|
set_callback(:process, :after, *methods, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def around_processing(*methods, &block)
|
||||||
|
set_callback(:process, :around, *methods, &block)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
25
test/unit/mailbox/callbacks_test.rb
Normal file
25
test/unit/mailbox/callbacks_test.rb
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
require_relative '../../test_helper'
|
||||||
|
|
||||||
|
class CallbackMailbox < ActionMailroom::Mailbox
|
||||||
|
before_processing { $before_processing = "Ran that!" }
|
||||||
|
after_processing { $after_processing = "Ran that too!" }
|
||||||
|
around_processing ->(r, block) { block.call; $around_processing = "Ran that as well!" }
|
||||||
|
|
||||||
|
def process
|
||||||
|
$processed = mail.subject
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ActionMailroom::Mailbox::CallbacksTest < ActiveSupport::TestCase
|
||||||
|
setup do
|
||||||
|
$before_processing = $after_processing = $around_processing = $processed = false
|
||||||
|
@inbound_email = create_inbound_email("welcome.eml")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "all callback types" do
|
||||||
|
CallbackMailbox.receive @inbound_email
|
||||||
|
assert_equal "Ran that!", $before_processing
|
||||||
|
assert_equal "Ran that too!", $after_processing
|
||||||
|
assert_equal "Ran that as well!", $around_processing
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue