1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Terminate processing if inbound email is marked as delivered in callback

This commit is contained in:
George Claghorn 2018-10-06 22:15:36 -04:00
parent 015c33f4cd
commit 7b95ebc9e4
3 changed files with 34 additions and 10 deletions

View file

@ -8,7 +8,7 @@ class ActionMailbox::Base
include ActionMailbox::Callbacks, ActionMailbox::Routing include ActionMailbox::Callbacks, ActionMailbox::Routing
attr_reader :inbound_email attr_reader :inbound_email
delegate :mail, :bounced!, to: :inbound_email delegate :mail, :delivered!, :bounced!, to: :inbound_email
delegate :logger, to: ActionMailbox delegate :logger, to: ActionMailbox
@ -35,6 +35,11 @@ class ActionMailbox::Base
# Overwrite in subclasses # Overwrite in subclasses
end end
def finished_processing?
inbound_email.delivered? || inbound_email.bounced?
end
def bounce_with(message) def bounce_with(message)
inbound_email.bounced! inbound_email.bounced!
message.deliver_later message.deliver_later

View file

@ -5,15 +5,9 @@ module ActionMailbox
extend ActiveSupport::Concern extend ActiveSupport::Concern
include ActiveSupport::Callbacks include ActiveSupport::Callbacks
TERMINATOR = ->(target, chain) do TERMINATOR = ->(mailbox, chain) do
terminate = true chain.call
mailbox.finished_processing?
catch(:abort) do
chain.call
terminate = target.inbound_email.bounced?
end
terminate
end end
included do included do

View file

@ -27,6 +27,23 @@ class BouncingCallbackMailbox < ActionMailbox::Base
end end
end end
class DiscardingCallbackMailbox < ActionMailbox::Base
before_processing { $before_processing = [ "Pre-discard" ] }
before_processing do
delivered!
$before_processing << "Discard"
end
before_processing { $before_processing << "Post-discard" }
after_processing { $after_processing = true }
def process
$processed = true
end
end
class ActionMailbox::Base::CallbacksTest < ActiveSupport::TestCase class ActionMailbox::Base::CallbacksTest < ActiveSupport::TestCase
setup do setup do
$before_processing = $after_processing = $around_processing = $processed = false $before_processing = $after_processing = $around_processing = $processed = false
@ -47,4 +64,12 @@ class ActionMailbox::Base::CallbacksTest < ActiveSupport::TestCase
assert_not $processed assert_not $processed
assert_not $after_processing assert_not $after_processing
end end
test "marking the inbound email as delivered in a callback terminates processing" do
DiscardingCallbackMailbox.receive @inbound_email
assert @inbound_email.delivered?
assert_equal [ "Pre-discard", "Discard" ], $before_processing
assert_not $processed
assert_not $after_processing
end
end end