From 95e06e6682b3bc7f813336142213697eac36e401 Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Thu, 7 Apr 2016 13:16:54 -0700 Subject: Disallow calling `#deliver_later` after local message modifications. They would be lost when the delivery job is enqueued, otherwise. Prevents a common, hard-to-find bug like: ```ruby message = Notifier.welcome(user, foo) message.message_id = my_generated_message_id message.deliver_later ``` The message_id is silently lost here! *Only the mailer arguments are passed to the delivery job.* This raises an exception now. Make modifications to the message within the mailer method or use a custom Active Job to manage delivery instead of using #deliver_later. --- actionmailer/test/message_delivery_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'actionmailer/test') diff --git a/actionmailer/test/message_delivery_test.rb b/actionmailer/test/message_delivery_test.rb index b834cdd08c..f06d69369f 100644 --- a/actionmailer/test/message_delivery_test.rb +++ b/actionmailer/test/message_delivery_test.rb @@ -93,4 +93,12 @@ class MessageDeliveryTest < ActiveSupport::TestCase @mail.deliver_later(queue: :another_queue) end end + + test 'deliver_later after accessing the message is disallowed' do + @mail.message # Load the message, which calls the mailer method. + + assert_raise RuntimeError do + @mail.deliver_later + end + end end -- cgit v1.2.3