Skip to content

Commit

Permalink
Support Mailer.deliver_foo(*args) as a synonym for `Mailer.foo(*arg…
Browse files Browse the repository at this point in the history
…s).deliver`.

This makes it easy to write e.g. `Mailer.expects(:deliver_foo)` when
testing code that calls the mailer.
  • Loading branch information
jonleighton committed Sep 28, 2012
1 parent aa8918a commit 7e0cf56
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions actionmailer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##

* Support `Mailer.deliver_foo(*args)` as a synonym for
`Mailer.foo(*args).deliver`. This makes it easy to write e.g.
`Mailer.expects(:deliver_foo)` when testing code that calls
the mailer. *Jon Leighton*

* Allow delivery method options to be set per mail instance *Aditya Sanghi*

If your smtp delivery settings are dynamic,
Expand Down
3 changes: 3 additions & 0 deletions actionmailer/lib/action_mailer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ module ActionMailer
# for delivery later:
#
# Notifier.welcome(david).deliver # sends the email
# Notifier.deliver_welcome(david) # synonym for the former
# mail = Notifier.welcome(david) # => a Mail::Message object
# mail.deliver # sends the email
#
Expand Down Expand Up @@ -487,6 +488,8 @@ def set_payload_for_mail(payload, mail) #:nodoc:
def method_missing(method_name, *args)
if action_methods.include?(method_name.to_s)
QueuedMessage.new(queue, self, method_name, *args)
elsif method_name.to_s =~ /^deliver_(.+)$/ && action_methods.include?($1)
public_send($1, *args).deliver
else
super
end
Expand Down
7 changes: 7 additions & 0 deletions actionmailer/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,13 @@ def welcome
assert_equal ["[email protected]"], DefaultFromMailer.welcome.from
end

test "Mailer.deliver_welcome calls Mailer.welcome.deliver" do
BaseMailer.deliveries.clear
BaseMailer.deliver_welcome(subject: 'omg')
assert_equal 1, BaseMailer.deliveries.length
assert_equal 'omg', BaseMailer.deliveries.first.subject
end

protected

# Execute the block setting the given values and restoring old values after
Expand Down

0 comments on commit 7e0cf56

Please sign in to comment.