aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/lib
diff options
context:
space:
mode:
authorGannon McGibbon <gannon.mcgibbon@gmail.com>2018-11-30 17:33:35 -0500
committerGannon McGibbon <gannon.mcgibbon@gmail.com>2018-12-04 11:00:34 -0500
commitf5050d998def98563f8fa4b381c09f563681f159 (patch)
tree0d32f8ccaddaf9bf7e1dd061658f58b8877f2f78 /actionmailer/lib
parent609c58bfa6df58b88e3eadebb1f1d49354541d24 (diff)
downloadrails-f5050d998def98563f8fa4b381c09f563681f159.tar.gz
rails-f5050d998def98563f8fa4b381c09f563681f159.tar.bz2
rails-f5050d998def98563f8fa4b381c09f563681f159.zip
Add MailDeliveryJob for unified mail delivery
Add `MailDeliveryJob` for delivering both regular and parameterized mail. Deprecate using `DeliveryJob` and `Parameterized::DeliveryJob`.
Diffstat (limited to 'actionmailer/lib')
-rw-r--r--actionmailer/lib/action_mailer.rb1
-rw-r--r--actionmailer/lib/action_mailer/base.rb2
-rw-r--r--actionmailer/lib/action_mailer/delivery_job.rb13
-rw-r--r--actionmailer/lib/action_mailer/mail_delivery_job.rb38
-rw-r--r--actionmailer/lib/action_mailer/message_delivery.rb10
-rw-r--r--actionmailer/lib/action_mailer/parameterized.rb18
-rw-r--r--actionmailer/lib/action_mailer/test_helper.rb4
7 files changed, 77 insertions, 9 deletions
diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb
index 69eae65d60..48f16180be 100644
--- a/actionmailer/lib/action_mailer.rb
+++ b/actionmailer/lib/action_mailer.rb
@@ -52,6 +52,7 @@ module ActionMailer
autoload :TestHelper
autoload :MessageDelivery
autoload :DeliveryJob
+ autoload :MailDeliveryJob
def self.eager_load!
super
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 5d41e5ff2a..8ddc90b9df 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -461,7 +461,7 @@ module ActionMailer
helper ActionMailer::MailHelper
- class_attribute :delivery_job, default: ::ActionMailer::DeliveryJob
+ class_attribute :delivery_job, default: ::ActionMailer::MailDeliveryJob
class_attribute :default_params, default: {
mime_version: "1.0",
charset: "UTF-8",
diff --git a/actionmailer/lib/action_mailer/delivery_job.rb b/actionmailer/lib/action_mailer/delivery_job.rb
index 8af2097819..f228006920 100644
--- a/actionmailer/lib/action_mailer/delivery_job.rb
+++ b/actionmailer/lib/action_mailer/delivery_job.rb
@@ -12,9 +12,16 @@ module ActionMailer
rescue_from StandardError, with: :handle_exception_with_mailer_class
- def perform(mailer, mail_method, delivery_method, params, *args) #:nodoc:
- mailer_class = params ? mailer.constantize.with(params) : mailer.constantize
- mailer_class.public_send(mail_method, *args).send(delivery_method)
+ before_perform do
+ ActiveSupport::Deprecation.warn <<~MSG.squish
+ Sending mail with DeliveryJob and Parameterized::DeliveryJob
+ is deprecated and will be removed in Rails 6.1.
+ Please use MailDeliveryJob instead.
+ MSG
+ end
+
+ def perform(mailer, mail_method, delivery_method, *args) #:nodoc:
+ mailer.constantize.public_send(mail_method, *args).send(delivery_method)
end
private
diff --git a/actionmailer/lib/action_mailer/mail_delivery_job.rb b/actionmailer/lib/action_mailer/mail_delivery_job.rb
new file mode 100644
index 0000000000..93778edfce
--- /dev/null
+++ b/actionmailer/lib/action_mailer/mail_delivery_job.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+require "active_job"
+
+module ActionMailer
+ # The <tt>ActionMailer::NewDeliveryJob</tt> class is used when you
+ # want to send emails outside of the request-response cycle. It supports
+ # sending either parameterized or normal mail.
+ #
+ # Exceptions are rescued and handled by the mailer class.
+ class MailDeliveryJob < ActiveJob::Base # :nodoc:
+ queue_as { ActionMailer::Base.deliver_later_queue_name }
+
+ rescue_from StandardError, with: :handle_exception_with_mailer_class
+
+ def perform(mailer, mail_method, delivery_method, args:, params: nil) #:nodoc:
+ mailer_class = params ? mailer.constantize.with(params) : mailer.constantize
+ mailer_class.public_send(mail_method, *args).send(delivery_method)
+ end
+
+ private
+ # "Deserialize" the mailer class name by hand in case another argument
+ # (like a Global ID reference) raised DeserializationError.
+ def mailer_class
+ if mailer = Array(@serialized_arguments).first || Array(arguments).first
+ mailer.constantize
+ end
+ end
+
+ def handle_exception_with_mailer_class(exception)
+ if klass = mailer_class
+ klass.handle_exception exception
+ else
+ raise exception
+ end
+ end
+ end
+end
diff --git a/actionmailer/lib/action_mailer/message_delivery.rb b/actionmailer/lib/action_mailer/message_delivery.rb
index 6b02076422..1e5cab6d47 100644
--- a/actionmailer/lib/action_mailer/message_delivery.rb
+++ b/actionmailer/lib/action_mailer/message_delivery.rb
@@ -135,10 +135,18 @@ module ActionMailer
"#deliver_later, 2. only touch the message *within your mailer " \
"method*, or 3. use a custom Active Job instead of #deliver_later."
else
- args = @mailer_class.name, @action.to_s, delivery_method.to_s, nil, *@args
job = @mailer_class.delivery_job
+ args = arguments_for(job, delivery_method)
job.set(options).perform_later(*args)
end
end
+
+ def arguments_for(delivery_job, delivery_method)
+ if delivery_job <= MailDeliveryJob
+ [@mailer_class.name, @action.to_s, delivery_method.to_s, args: @args]
+ else
+ [@mailer_class.name, @action.to_s, delivery_method.to_s, *@args]
+ end
+ end
end
end
diff --git a/actionmailer/lib/action_mailer/parameterized.rb b/actionmailer/lib/action_mailer/parameterized.rb
index 0b0afd6ad4..999435919e 100644
--- a/actionmailer/lib/action_mailer/parameterized.rb
+++ b/actionmailer/lib/action_mailer/parameterized.rb
@@ -121,6 +121,12 @@ module ActionMailer
end
end
+ class DeliveryJob < ActionMailer::DeliveryJob # :nodoc:
+ def perform(mailer, mail_method, delivery_method, params, *args)
+ mailer.constantize.with(params).public_send(mail_method, *args).send(delivery_method)
+ end
+ end
+
class MessageDelivery < ActionMailer::MessageDelivery # :nodoc:
def initialize(mailer_class, action, params, *args)
super(mailer_class, action, *args)
@@ -139,11 +145,19 @@ module ActionMailer
if processed?
super
else
- args = @mailer_class.name, @action.to_s, delivery_method.to_s, @params, *@args
- job = @mailer_class.delivery_job
+ job = @mailer_class.delivery_job
+ args = arguments_for(job, delivery_method)
job.set(options).perform_later(*args)
end
end
+
+ def arguments_for(delivery_job, delivery_method)
+ if delivery_job <= MailDeliveryJob
+ [@mailer_class.name, @action.to_s, delivery_method.to_s, params: @params, args: @args]
+ else
+ [@mailer_class.name, @action.to_s, delivery_method.to_s, @params, *@args]
+ end
+ end
end
end
end
diff --git a/actionmailer/lib/action_mailer/test_helper.rb b/actionmailer/lib/action_mailer/test_helper.rb
index 3eb61c2363..e222301dff 100644
--- a/actionmailer/lib/action_mailer/test_helper.rb
+++ b/actionmailer/lib/action_mailer/test_helper.rb
@@ -125,9 +125,9 @@ module ActionMailer
# end
def assert_enqueued_email_with(mailer, method, args: nil, queue: "mailers", &block)
args = if args.is_a?(Hash)
- [mailer.to_s, method.to_s, "deliver_now", args]
+ [mailer.to_s, method.to_s, "deliver_now", params: args, args: []]
else
- [mailer.to_s, method.to_s, "deliver_now", nil, *args]
+ [mailer.to_s, method.to_s, "deliver_now", args: Array(args)]
end
assert_enqueued_with(job: mailer.delivery_job, args: args, queue: queue, &block)
end