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
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ## ## 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* * Allow delivery method options to be set per mail instance *Aditya Sanghi*


If your smtp delivery settings are dynamic, If your smtp delivery settings are dynamic,
Expand Down
3 changes: 3 additions & 0 deletions actionmailer/lib/action_mailer/base.rb
Expand Up @@ -142,6 +142,7 @@ module ActionMailer
# for delivery later: # for delivery later:
# #
# Notifier.welcome(david).deliver # sends the email # Notifier.welcome(david).deliver # sends the email
# Notifier.deliver_welcome(david) # synonym for the former

This comment has been minimized.

Copy link
@phlipper

phlipper Oct 28, 2012

This is an interesting addition, considering it was deprecated in ActionMailer 3.0 - see line 108 and this post among other resources.

I don't personally care for this addition and I'm interested to hear what other opinions are on this.

This comment has been minimized.

Copy link
@jonleighton

jonleighton Oct 28, 2012

Author Member

Interesting. I wasn't aware of that. @josevalim wdyt about this? would you prefer it to be reverted?

This comment has been minimized.

Copy link
@phlipper

phlipper Oct 28, 2012

@jonleighton what is your plan for this? It was added a month ago, but an ack of master shows the only place it is being used is the test that was added in this commit.

I personally agreed with the decision to remove the "magic" methods. If there was a reason to re-introduce them for some reason, one could argue that the full original API should come back.

What do you think?

This comment has been minimized.

Copy link
@phlipper

phlipper Oct 30, 2012

@josevalim do you have any opinions on this?

This comment has been minimized.

Copy link
@dhh

dhh Oct 30, 2012

Member

I'm -1 on this. It's imo easy enough to do Mailer.expects(:welcome) and then keep mocking from there. I don't like having two ways of doing the same thing purely to make it marginally easier to mock.

This comment has been minimized.

Copy link
@jonleighton

jonleighton Oct 30, 2012

Author Member
Mailer.expects(:deliver_welcome)
Mailer.expects(:welcome).returns(mock(deliver: true))

I think the former tells the story of what's happening much more clearly than the latter.

@dhh you have also told me that "multiple ways of doing things" is the ruby way in the past (in an entirely different conversation). so not convinced by that argument.

However, I really don't care enough to argue about it, so please revert if you feel strongly.

This comment has been minimized.

Copy link
@dhh

dhh via email Oct 30, 2012

Member
# mail = Notifier.welcome(david) # => a Mail::Message object # mail = Notifier.welcome(david) # => a Mail::Message object
# mail.deliver # sends the email # 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) def method_missing(method_name, *args)
if action_methods.include?(method_name.to_s) if action_methods.include?(method_name.to_s)
QueuedMessage.new(queue, self, method_name, *args) QueuedMessage.new(queue, self, method_name, *args)
elsif method_name.to_s =~ /^deliver_(.+)$/ && action_methods.include?($1)
public_send($1, *args).deliver
else else
super super
end end
Expand Down
7 changes: 7 additions & 0 deletions actionmailer/test/base_test.rb
Expand Up @@ -662,6 +662,13 @@ def welcome
assert_equal ["robert.pankowecki@gmail.com"], DefaultFromMailer.welcome.from assert_equal ["robert.pankowecki@gmail.com"], DefaultFromMailer.welcome.from
end 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 protected


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

0 comments on commit 7e0cf56

Please sign in to comment.