From 08b07b60b6d91a2f7bba5eec1e9b1d26599c578a Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Sun, 2 May 2010 11:30:10 +1000 Subject: Adding ability to pass proc's to the ActionMailer class default method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionmailer/lib/action_mailer/base.rb | 7 ++++++- actionmailer/test/base_test.rb | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) (limited to 'actionmailer') 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 -- cgit v1.2.3