diff options
-rw-r--r-- | lib/action_mailbox/callbacks.rb | 13 | ||||
-rw-r--r-- | test/unit/mailbox/callbacks_test.rb | 22 |
2 files changed, 34 insertions, 1 deletions
diff --git a/lib/action_mailbox/callbacks.rb b/lib/action_mailbox/callbacks.rb index 33caaafd2a..e8bf2ffd55 100644 --- a/lib/action_mailbox/callbacks.rb +++ b/lib/action_mailbox/callbacks.rb @@ -5,8 +5,19 @@ module ActionMailbox extend ActiveSupport::Concern include ActiveSupport::Callbacks + TERMINATOR = ->(target, chain) do + terminate = true + + catch(:abort) do + chain.call + terminate = target.inbound_email.bounced? + end + + terminate + end + included do - define_callbacks :process + define_callbacks :process, terminator: TERMINATOR end module ClassMethods diff --git a/test/unit/mailbox/callbacks_test.rb b/test/unit/mailbox/callbacks_test.rb index b6cfef9868..fccd89b9df 100644 --- a/test/unit/mailbox/callbacks_test.rb +++ b/test/unit/mailbox/callbacks_test.rb @@ -10,6 +10,21 @@ class CallbackMailbox < ActionMailbox::Base end end +class BouncingCallbackMailbox < ActionMailbox::Base + before_processing { $before_processing = [ "Pre-bounce" ] } + + before_processing do + bounce_with BounceMailer.bounce(to: mail.from) + $before_processing << "Bounce" + end + + before_processing { $before_processing << "Post-bounce" } + + def process + $processed = mail.subject + end +end + class ActionMailbox::Base::CallbacksTest < ActiveSupport::TestCase setup do $before_processing = $after_processing = $around_processing = $processed = false @@ -22,4 +37,11 @@ class ActionMailbox::Base::CallbacksTest < ActiveSupport::TestCase assert_equal "Ran that too!", $after_processing assert_equal "Ran that as well!", $around_processing end + + test "bouncing in a callback terminates processing" do + BouncingCallbackMailbox.receive @inbound_email + assert @inbound_email.bounced? + assert_equal [ "Pre-bounce", "Bounce" ], $before_processing + assert_not $processed + end end |