Allow mailers to configure their delivery job

Setting delivery_job on a mailer class will cause MessageDelivery to use
the specified job instead of ActionMailer::DeliveryJob:

    class MyMailer < ApplicationMailer
      self.delivery_job = MyCustomDeliveryJob

      ...
    end
This commit is contained in:
Matthew Mongeau 2017-06-15 16:12:44 +09:00
parent f2af91a790
commit d9bbde09fc
4 changed files with 26 additions and 1 deletions

View File

@ -1 +1,11 @@
* Allow ActionMailer classes to configure their delivery job
class MyMailer < ApplicationMailer
self.delivery_job = MyCustomDeliveryJob
...
end
*Matthew Mongeau*
Please check [5-1-stable](https://github.com/rails/rails/blob/5-1-stable/actionmailer/CHANGELOG.md) for previous changes.

View File

@ -459,6 +459,7 @@ module ActionMailer
helper ActionMailer::MailHelper
class_attribute :delivery_job, default: ::ActionMailer::DeliveryJob
class_attribute :default_params, default: {
mime_version: "1.0",
charset: "UTF-8",

View File

@ -118,7 +118,8 @@ module ActionMailer
"method*, or 3. use a custom Active Job instead of #deliver_later."
else
args = @mailer_class.name, @action.to_s, delivery_method.to_s, *@args
::ActionMailer::DeliveryJob.set(options).perform_later(*args)
job = @mailer_class.delivery_job
job.set(options).perform_later(*args)
end
end
end

View File

@ -95,6 +95,19 @@ class MessageDeliveryTest < ActiveSupport::TestCase
end
end
test "should enqueue the job with the correct delivery job" do
old_delivery_job = DelayedMailer.delivery_job
DelayedMailer.delivery_job = DummyJob
assert_performed_with(job: DummyJob, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3]) do
@mail.deliver_later
end
DelayedMailer.delivery_job = old_delivery_job
end
class DummyJob < ActionMailer::DeliveryJob; end
test "can override the queue when enqueuing mail" do
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3], queue: "another_queue") do
@mail.deliver_later(queue: :another_queue)