aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer
diff options
context:
space:
mode:
authorMikel Lindsaar <raasdnil@gmail.com>2010-05-02 11:30:10 +1000
committerJosé Valim <jose.valim@gmail.com>2010-05-02 11:56:08 +0200
commit08b07b60b6d91a2f7bba5eec1e9b1d26599c578a (patch)
treeb315e94b226abd7485c6e52260a0f70213cbd5e2 /actionmailer
parent256a15c23581865559cc758c2e377cd395cc05b3 (diff)
downloadrails-08b07b60b6d91a2f7bba5eec1e9b1d26599c578a.tar.gz
rails-08b07b60b6d91a2f7bba5eec1e9b1d26599c578a.tar.bz2
rails-08b07b60b6d91a2f7bba5eec1e9b1d26599c578a.zip
Adding ability to pass proc's to the ActionMailer class default method
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'actionmailer')
-rw-r--r--actionmailer/lib/action_mailer/base.rb7
-rw-r--r--actionmailer/test/base_test.rb17
2 files changed, 23 insertions, 1 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index e566132f4e..3a49da5984 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -528,8 +528,13 @@ module ActionMailer #:nodoc:
content_type = headers[:content_type]
parts_order = headers[:parts_order]
+ # Call all the procs (if any)
+ default_values = self.class.default.merge(self.class.default) do |k,v|
+ v.respond_to?(:call) ? v.call : v
+ end
+
# Handle defaults
- headers = headers.reverse_merge(self.class.default)
+ headers = headers.reverse_merge(default_values)
headers[:subject] ||= default_i18n_subject
# Apply charset at the beginning so all fields are properly quoted
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb
index 8e69073009..81e41dc8d4 100644
--- a/actionmailer/test/base_test.rb
+++ b/actionmailer/test/base_test.rb
@@ -113,6 +113,15 @@ class BaseTest < ActiveSupport::TestCase
end
end
end
+
+ class ProcMailer < ActionMailer::Base
+ default :to => 'system@test.lindsaar.net',
+ 'X-Proc-Method' => Proc.new { Time.now.to_i.to_s }
+
+ def welcome
+ mail
+ end
+ end
test "method call to mail does not raise error" do
assert_nothing_raised { BaseMailer.welcome }
@@ -560,6 +569,14 @@ class BaseTest < ActiveSupport::TestCase
MyInterceptor.expects(:delivering_email).with(mail)
mail.deliver
end
+
+ test "being able to put proc's into the defaults hash and they get evaluated on mail sending" do
+ mail1 = ProcMailer.welcome
+ yesterday = 1.day.ago
+ Time.stubs(:now).returns(yesterday)
+ mail2 = ProcMailer.welcome
+ assert(mail1['X-Proc-Method'].to_s.to_i > mail2['X-Proc-Method'].to_s.to_i)
+ end
protected