Skip to content
This repository
Browse code

Support `Mailer.deliver_foo(*args)` as a synonym for `Mailer.foo(*arg…

…s).deliver`.

This makes it easy to write e.g. `Mailer.expects(:deliver_foo)` when
testing code that calls the mailer.
  • Loading branch information...
commit 7e0cf563639bc7508da381b1b8321c7a89be1aa8 1 parent aa8918a
Jon Leighton authored September 28, 2012
5  actionmailer/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,10 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+* Support `Mailer.deliver_foo(*args)` as a synonym for
  4
+  `Mailer.foo(*args).deliver`. This makes it easy to write e.g.
  5
+  `Mailer.expects(:deliver_foo)` when testing code that calls
  6
+  the mailer. *Jon Leighton*
  7
+
3 8
 * Allow delivery method options to be set per mail instance *Aditya Sanghi*
4 9
 
5 10
   If your smtp delivery settings are dynamic,
3  actionmailer/lib/action_mailer/base.rb
@@ -142,6 +142,7 @@ module ActionMailer
142 142
   # for delivery later:
143 143
   #
144 144
   #   Notifier.welcome(david).deliver # sends the email
  145
+  #   Notifier.deliver_welcome(david) # synonym for the former
145 146
   #   mail = Notifier.welcome(david)  # => a Mail::Message object
146 147
   #   mail.deliver                    # sends the email
147 148
   #
@@ -487,6 +488,8 @@ def set_payload_for_mail(payload, mail) #:nodoc:
487 488
       def method_missing(method_name, *args)
488 489
         if action_methods.include?(method_name.to_s)
489 490
           QueuedMessage.new(queue, self, method_name, *args)
  491
+        elsif method_name.to_s =~ /^deliver_(.+)$/ && action_methods.include?($1)
  492
+          public_send($1, *args).deliver
490 493
         else
491 494
           super
492 495
         end
7  actionmailer/test/base_test.rb
@@ -662,6 +662,13 @@ def welcome
662 662
     assert_equal ["robert.pankowecki@gmail.com"], DefaultFromMailer.welcome.from
663 663
   end
664 664
 
  665
+  test "Mailer.deliver_welcome calls Mailer.welcome.deliver" do
  666
+    BaseMailer.deliveries.clear
  667
+    BaseMailer.deliver_welcome(subject: 'omg')
  668
+    assert_equal 1, BaseMailer.deliveries.length
  669
+    assert_equal 'omg', BaseMailer.deliveries.first.subject
  670
+  end
  671
+
665 672
   protected
666 673
 
667 674
     # Execute the block setting the given values and restoring old values after

0 notes on commit 7e0cf56

Phil Cohen

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.

Jon Leighton

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

Phil Cohen

@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?

David Heinemeier Hansson

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.

Jon Leighton
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.

David Heinemeier Hansson
Please sign in to comment.
Something went wrong with that request. Please try again.