fix(mailer): observers on balancer and balanced delivery methods
- Les observers doivent être déclarés *avant* le chargement de nos delivery methods (Sendinblue & Dolist), sinon ils seront ignorés par ces derniers - dans le balancer, on ne peut pas appeler `.deliver` une seconde fois, sinon les observers (et interceptors) sont invoqués deux fois.
This commit is contained in:
parent
1766288c51
commit
05b517a847
3 changed files with 33 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue