From ad6e56318e9202f632d8700fd72bce3ea9534395 Mon Sep 17 00:00:00 2001 From: Scott Windsor Date: Sun, 27 Sep 2009 12:26:27 -0700 Subject: [PATCH] Adding layout accessor to override any default or base class layout with new tests --- actionmailer/lib/action_mailer/base.rb | 5 +++++ actionmailer/test/mail_layout_test.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 0 deletions(-) diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 84997de..8fa9d35 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -354,6 +354,10 @@ module ActionMailer #:nodoc: # have multiple mailer methods share the same template. adv_attr_accessor :template + # Specify the layout to use for the current message. This overrides any + # default layout or action mailer layout. + adv_attr_accessor :layout + # Override the mailer name, which defaults to an inflected version of the # mailer's class name. If you want to use a template in a non-standard # location, you can use this to specify that location. @@ -560,6 +564,7 @@ module ActionMailer #:nodoc: if opts[:file] && (opts[:file] !~ /\// && !opts[:file].respond_to?(:render)) opts[:file] = "#{mailer_name}/#{opts[:file]}" end + opts[:layout] = self.layout unless self.layout.nil? begin old_template, @template = @template, initialize_template_class(body) diff --git a/actionmailer/test/mail_layout_test.rb b/actionmailer/test/mail_layout_test.rb index 50901f5..9d134fb 100644 --- a/actionmailer/test/mail_layout_test.rb +++ b/actionmailer/test/mail_layout_test.rb @@ -14,6 +14,14 @@ class AutoLayoutMailer < ActionMailer::Base body render(:inline => "Hello, <%= @world %>", :layout => 'spam', :body => { :world => "Earth" }) end + def layout_override_spam(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + layout "spam" + body render(:inline => "Hello, <%= @world %>", :body => { :world => "Earth" }) + end + def nolayout(recipient) recipients recipient subject "You have a mail" @@ -21,6 +29,14 @@ class AutoLayoutMailer < ActionMailer::Base body render(:inline => "Hello, <%= @world %>", :layout => false, :body => { :world => "Earth" }) end + def layout_override_none(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + layout false + body render(:inline => "Hello, <%= @world %>", :body => { :world => "Earth" }) + end + def multipart(recipient, type = nil) recipients recipient subject "You have a mail" @@ -106,11 +122,21 @@ class LayoutMailerTest < Test::Unit::TestCase assert_equal "Spammer layout Hello, Earth", mail.body.strip end + def test_should_override_layout_specified + mail = AutoLayoutMailer.create_layout_override_spam(@recipient) + assert_equal "Spammer layout Hello, Earth", mail.body.strip + end + def test_should_respect_layout_false mail = AutoLayoutMailer.create_nolayout(@recipient) assert_equal "Hello, Earth", mail.body.strip end + def test_should_respect_override_layout_false + mail = AutoLayoutMailer.create_layout_override_none(@recipient) + assert_equal "Hello, Earth", mail.body.strip + end + def test_explicit_class_layout mail = ExplicitLayoutMailer.create_signup(@recipient) assert_equal "Spammer layout We do not spam", mail.body.strip -- 1.6.3.2