Skip to content

Commit

Permalink
Deliver parameterized mail with DeliveryJob
Browse files Browse the repository at this point in the history
Deliver parameterized mail with `ActionMailer::DeliveryJob`
and remove `ActionMailer::Parameterized::DeliveryJob`.
  • Loading branch information
gmcgibbon committed Nov 22, 2018
1 parent 548bd50 commit 60339da
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 51 deletions.
15 changes: 4 additions & 11 deletions actionmailer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Deliver parameterized mail with `ActionMailer::DeliveryJob` and remove `ActionMailer::Parameterized::DeliveryJob`.

*Gannon McGibbon*

* Fix ActionMailer assertions not working when a Mail defines
a custom delivery job class

Expand All @@ -8,17 +12,6 @@

*Marcus Ilgner*

* Allow ActionMailer classes to configure the parameterized delivery job
Example:
```
class MyMailer < ApplicationMailer
self.parameterized_delivery_job = MyCustomDeliveryJob
...
end
```
*Luke Pearce*
* `ActionDispatch::IntegrationTest` includes `ActionMailer::TestHelper` module by default.

*Ricardo Díaz*
Expand Down
1 change: 0 additions & 1 deletion actionmailer/lib/action_mailer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,6 @@ def _protected_ivars # :nodoc:
helper ActionMailer::MailHelper

class_attribute :delivery_job, default: ::ActionMailer::DeliveryJob
class_attribute :parameterized_delivery_job, default: ::ActionMailer::Parameterized::DeliveryJob
class_attribute :default_params, default: {
mime_version: "1.0",
charset: "UTF-8",
Expand Down
5 changes: 3 additions & 2 deletions actionmailer/lib/action_mailer/delivery_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class DeliveryJob < ActiveJob::Base # :nodoc:

rescue_from StandardError, with: :handle_exception_with_mailer_class

def perform(mailer, mail_method, delivery_method, *args) #:nodoc:
mailer.constantize.public_send(mail_method, *args).send(delivery_method)
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)
end

private
Expand Down
2 changes: 1 addition & 1 deletion actionmailer/lib/action_mailer/message_delivery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def enqueue_delivery(delivery_method, options = {})
"#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, *@args
args = @mailer_class.name, @action.to_s, delivery_method.to_s, nil, *@args
job = @mailer_class.delivery_job
job.set(options).perform_later(*args)
end
Expand Down
8 changes: 1 addition & 7 deletions actionmailer/lib/action_mailer/parameterized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,10 @@ def enqueue_delivery(delivery_method, options = {})
super
else
args = @mailer_class.name, @action.to_s, delivery_method.to_s, @params, *@args
job = @mailer_class.parameterized_delivery_job
job = @mailer_class.delivery_job
job.set(options).perform_later(*args)
end
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
end
end
14 changes: 5 additions & 9 deletions actionmailer/lib/action_mailer/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,12 @@ def assert_enqueued_emails(number, &block)
# end
# end
def assert_enqueued_email_with(mailer, method, args: nil, queue: "mailers", &block)
if args.is_a? Hash
job = mailer.parameterized_delivery_job
args = [mailer.to_s, method.to_s, "deliver_now", args]
args = if args.is_a?(Hash)
[mailer.to_s, method.to_s, "deliver_now", args]
else
job = mailer.delivery_job
args = [mailer.to_s, method.to_s, "deliver_now", *args]
[mailer.to_s, method.to_s, "deliver_now", nil, *args]
end

assert_enqueued_with(job: job, args: args, queue: queue, &block)
assert_enqueued_with(job: mailer.delivery_job, args: args, queue: queue, &block)
end

# Asserts that no emails are enqueued for later delivery.
Expand All @@ -159,8 +156,7 @@ def assert_no_enqueued_emails(&block)
def delivery_job_filter(job)
job_class = job.is_a?(Hash) ? job.fetch(:job) : job.class

Base.descendants.map(&:delivery_job).include?(job_class) ||
Base.descendants.map(&:parameterized_delivery_job).include?(job_class)
Base.descendants.map(&:delivery_job).include?(job_class)
end
end
end
14 changes: 7 additions & 7 deletions actionmailer/test/message_delivery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,34 @@ def test_should_enqueue_and_run_correctly_in_activejob
end

test "should enqueue the email with :deliver_now delivery method" do
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3]) do
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now", nil, 1, 2, 3]) do
@mail.deliver_later
end
end

test "should enqueue the email with :deliver_now! delivery method" do
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now!", 1, 2, 3]) do
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now!", nil, 1, 2, 3]) do
@mail.deliver_later!
end
end

test "should enqueue a delivery with a delay" do
travel_to Time.new(2004, 11, 24, 01, 04, 44) do
assert_performed_with(job: ActionMailer::DeliveryJob, at: Time.current + 10.minutes, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3]) do
assert_performed_with(job: ActionMailer::DeliveryJob, at: Time.current + 10.minutes, args: ["DelayedMailer", "test_message", "deliver_now", nil, 1, 2, 3]) do
@mail.deliver_later wait: 10.minutes
end
end
end

test "should enqueue a delivery at a specific time" do
later_time = Time.current + 1.hour
assert_performed_with(job: ActionMailer::DeliveryJob, at: later_time, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3]) do
assert_performed_with(job: ActionMailer::DeliveryJob, at: later_time, args: ["DelayedMailer", "test_message", "deliver_now", nil, 1, 2, 3]) do
@mail.deliver_later wait_until: later_time
end
end

test "should enqueue the job on the correct queue" do
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3], queue: "test_queue") do
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now", nil, 1, 2, 3], queue: "test_queue") do
@mail.deliver_later
end
end
Expand All @@ -100,7 +100,7 @@ def test_should_enqueue_and_run_correctly_in_activejob
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
assert_performed_with(job: DummyJob, args: ["DelayedMailer", "test_message", "deliver_now", nil, 1, 2, 3]) do
@mail.deliver_later
end

Expand All @@ -110,7 +110,7 @@ def test_should_enqueue_and_run_correctly_in_activejob
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
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now", nil, 1, 2, 3], queue: "another_queue") do
@mail.deliver_later(queue: :another_queue)
end
end
Expand Down
38 changes: 29 additions & 9 deletions actionmailer/test/parameterized_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
class ParameterizedTest < ActiveSupport::TestCase
include ActiveJob::TestHelper

class DummyDeliveryJob < ActionMailer::DeliveryJob
end

setup do
@previous_logger = ActiveJob::Base.logger
ActiveJob::Base.logger = Logger.new(nil)
Expand Down Expand Up @@ -35,7 +38,13 @@ class ParameterizedTest < ActiveSupport::TestCase
end

test "enqueue the email with params" do
assert_performed_with(job: ActionMailer::Parameterized::DeliveryJob, args: ["ParamsMailer", "invitation", "deliver_now", { inviter: "david@basecamp.com", invitee: "jason@basecamp.com" } ]) do
args = [
"ParamsMailer",
"invitation",
"deliver_now",
{ inviter: "david@basecamp.com", invitee: "jason@basecamp.com" },
]
assert_performed_with(job: ActionMailer::DeliveryJob, args: args) do
@mail.deliver_later
end
end
Expand All @@ -55,15 +64,26 @@ class ParameterizedTest < ActiveSupport::TestCase
end

test "should enqueue a parameterized request with the correct delivery job" do
old_delivery_job = ParamsMailer.parameterized_delivery_job
ParamsMailer.parameterized_delivery_job = ParameterizedDummyJob

assert_performed_with(job: ParameterizedDummyJob, args: ["ParamsMailer", "invitation", "deliver_now", { inviter: "david@basecamp.com", invitee: "jason@basecamp.com" } ]) do
@mail.deliver_later
args = [
"ParamsMailer",
"invitation",
"deliver_now",
{ inviter: "david@basecamp.com", invitee: "jason@basecamp.com" },
]

with_delivery_job DummyDeliveryJob do
assert_performed_with(job: DummyDeliveryJob, args: args) do
@mail.deliver_later
end
end

ParamsMailer.parameterized_delivery_job = old_delivery_job
end

class ParameterizedDummyJob < ActionMailer::Parameterized::DeliveryJob; end
private

def with_delivery_job(job)
old_delivery_job = ParamsMailer.delivery_job
ParamsMailer.delivery_job = job
ensure
ParamsMailer.delivery_job = old_delivery_job
end
end
4 changes: 0 additions & 4 deletions actionmailer/test/test_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ def test_parameter_args
class CustomDeliveryJob < ActionMailer::DeliveryJob
end

class CustomParameterizedDeliveryJob < ActionMailer::Parameterized::DeliveryJob
end

class CustomDeliveryMailer < TestHelperMailer
self.delivery_job = CustomDeliveryJob
self.parameterized_delivery_job = CustomParameterizedDeliveryJob
end

class TestHelperMailerTest < ActionMailer::TestCase
Expand Down

0 comments on commit 60339da

Please sign in to comment.