Merge pull request #8403 from colinux/fix-balancer-mailer-observers

fix(mailers): observers for balancer and balanced delivery methods
This commit is contained in:
Colin Darie 2023-01-11 09:44:13 +01:00 committed by GitHub
commit ecd24378d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 3 deletions

View file

@ -25,7 +25,16 @@ class BalancerDeliveryMethod
def deliver!(mail)
balanced_delivery_method = delivery_method(mail)
ApplicationMailer.wrap_delivery_behavior(mail, balanced_delivery_method)
mail.deliver
# Because we don't want to invoke observers or interceptors twice,
# we can't call again `mail.deliver` here to send the email with balanced method
# (it was first called before by deliver_now in ActiveJob or application code, which leads us here).
#
# Instead, we directly deliver the email from the handler (set by the wrapper above)
# like Mail::Message.deliver does.
#
# See https://github.com/mikel/mail/blob/199a76bed3fc518508b46135691914a1cfd8bff8/lib/mail/message.rb#L250
mail.delivery_handler.deliver_mail(mail) { mail.send :do_delivery }
end
private

View file

@ -1,3 +1,6 @@
Rails.application.configure do
config.action_mailer.observers = ['EmailDeliveryObserver']
# Must be registered *before* loading custom delivery methods
# otherwise the observer won't be invoked.
#
ActiveSupport.on_load(:action_mailer) do |mailer|
mailer.register_observer EmailDeliveryObserver
end

View file

@ -83,6 +83,24 @@ RSpec.describe BalancerDeliveryMethod do
end
end
context 'when observers are configured' do
let(:observer) { double("Observer") }
before do
allow(observer).to receive(:delivered_email)
ActionMailer::Base.register_observer(observer)
end
after do
ActionMailer::Base.unregister_observer(observer)
end
it 'invoke the observer exactly once' do
mail = ExampleMailer.greet('Joshua').deliver_now
expect(observer).to have_received(:delivered_email).with(mail).once
end
end
# Helpers
def have_been_delivered_using(delivery_class)