Permalink
Browse files

`mail()` without arguments is a getter for the current mail.

This behavior is documented in our guides (http://edgeguides.rubyonrails.org/action_mailer_basics.html#action-mailer-callbacks)
but was broken in the past. This commit short curcuits
the `mail` method if:

  1. mail() was previously called
  2. no headers are passed
  3. no block is passed

Closes #13090.

/cc @pixeltrix

Conflicts:
	actionmailer/CHANGELOG.md
  • Loading branch information...
1 parent 5f24b78 commit 0e2e51ca2fe668252e8b16acf4e6e0232773f08d @senny senny committed Dec 2, 2013
Showing with 45 additions and 0 deletions.
  1. +22 −0 actionmailer/CHANGELOG.md
  2. +2 −0 actionmailer/lib/action_mailer/base.rb
  3. +21 −0 actionmailer/test/base_test.rb
View
@@ -1,5 +1,27 @@
+* Calling `mail()` without arguments serves as getter for the current mail
+ message and keeps previously set headers.
+
+ Example:
+
+ class MailerWithCallback < ActionMailer::Base
+ after_action :a_callback
+
+ def welcome
+ mail subject: "subject", to: ["joe@example.com"]
+ end
+
+ def a_callback
+ mail # => returns the current mail message
+ end
+ end
+
+ *Yves Senn*
+
## Rails 4.0.1 (November 01, 2013) ##
+* Instrument the generation of Action Mailer messages. The time it takes to
+ generate a message is written to the log.
+
* Invoke mailer defaults as procs only if they are procs, do not convert
with `to_proc`. That an object is convertible to a proc does not mean it's
meant to be always used as a proc.
@@ -661,6 +661,8 @@ def attachments
# end
#
def mail(headers = {}, &block)
+ return @_message if @_mail_was_called && headers.blank? && !block
+
@_mail_was_called = true
m = @_message
@@ -671,6 +671,27 @@ def welcome
assert_equal ["robert.pankowecki@gmail.com"], DefaultFromMailer.welcome.from
end
+ test "mail() without arguments serves as getter for the current mail message" do
+ class MailerWithCallback < ActionMailer::Base
+ after_action :a_callback
+
+ def welcome
+ headers('X-Special-Header' => 'special indeed!')
+ mail subject: "subject", body: "hello world", to: ["joe@example.com"]
+ end
+
+ def a_callback
+ mail.to << "jane@example.com"
+ end
+ end
+
+ mail = MailerWithCallback.welcome
+ assert_equal "subject", mail.subject
+ assert_equal ["joe@example.com", "jane@example.com"], mail.to
+ assert_equal "hello world", mail.body.encoded.strip
+ assert_equal "special indeed!", mail["X-Special-Header"].to_s
+ end
+
protected
# Execute the block setting the given values and restoring old values after

0 comments on commit 0e2e51c

Please sign in to comment.