From 3cf773b187e803e16b8237e5923fa4c1139cde8a Mon Sep 17 00:00:00 2001 From: James Mead Date: Fri, 29 Aug 2008 15:08:16 -0500 Subject: ActionMailer should respond_to? to methods handled by method_missing [#700 state:resolved] Signed-off-by: Joshua Peek --- actionmailer/lib/action_mailer/base.rb | 21 ++++++++++---- actionmailer/test/mail_service_test.rb | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 5 deletions(-) (limited to 'actionmailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 72c94529b5..5b3c560390 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -374,11 +374,16 @@ module ActionMailer #:nodoc: alias_method :controller_name, :mailer_name alias_method :controller_path, :mailer_name - def method_missing(method_symbol, *parameters)#:nodoc: - case method_symbol.id2name - when /^create_([_a-z]\w*)/ then new($1, *parameters).mail - when /^deliver_([_a-z]\w*)/ then new($1, *parameters).deliver! - when "new" then nil + def respond_to?(method_symbol, include_private = false) #:nodoc: + matches_dynamic_method?(method_symbol) || super + end + + def method_missing(method_symbol, *parameters) #:nodoc: + match = matches_dynamic_method?(method_symbol) + case match[1] + when 'create' then new(match[2], *parameters).mail + when 'deliver' then new(match[2], *parameters).deliver! + when 'new' then nil else super end end @@ -424,6 +429,12 @@ module ActionMailer #:nodoc: def template_root=(root) self.view_paths = ActionView::Base.process_view_paths(root) end + + private + def matches_dynamic_method?(method_name) #:nodoc: + method_name = method_name.to_s + /(create|deliver)_([_a-z]\w*)/.match(method_name) || /^(new)$/.match(method_name) + end end # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/mail_service_test.rb index 882b07d675..f57c6f3fb8 100644 --- a/actionmailer/test/mail_service_test.rb +++ b/actionmailer/test/mail_service_test.rb @@ -968,3 +968,55 @@ class MethodNamingTest < Test::Unit::TestCase end end end + +class RespondToTest < Test::Unit::TestCase + class RespondToMailer < ActionMailer::Base; end + + def setup + set_delivery_method :test + end + + def teardown + restore_delivery_method + end + + def test_should_respond_to_new + assert RespondToMailer.respond_to?(:new) + end + + def test_should_respond_to_create_with_template_suffix + assert RespondToMailer.respond_to?(:create_any_old_template) + end + + def test_should_respond_to_deliver_with_template_suffix + assert RespondToMailer.respond_to?(:deliver_any_old_template) + end + + def test_should_not_respond_to_new_with_template_suffix + assert !RespondToMailer.respond_to?(:new_any_old_template) + end + + def test_should_not_respond_to_create_with_template_suffix_unless_it_is_separated_by_an_underscore + assert !RespondToMailer.respond_to?(:createany_old_template) + end + + def test_should_not_respond_to_deliver_with_template_suffix_unless_it_is_separated_by_an_underscore + assert !RespondToMailer.respond_to?(:deliverany_old_template) + end + + def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_uppercase_letter + assert !RespondToMailer.respond_to?(:create_Any_old_template) + end + + def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_uppercase_letter + assert !RespondToMailer.respond_to?(:deliver_Any_old_template) + end + + def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_digit + assert !RespondToMailer.respond_to?(:create_1_template) + end + + def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_digit + assert !RespondToMailer.respond_to?(:deliver_1_template) + end +end -- cgit v1.2.3 From e9a8e0053be3b293ab89fb584f1d660063f107aa Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 31 Aug 2008 19:11:15 +0100 Subject: Add layout functionality to mailers. Mailer layouts behaves just like controller layouts, except layout names need to have '_mailer' postfix for them to be automatically picked up. --- actionmailer/CHANGELOG | 5 ++ actionmailer/lib/action_mailer/base.rb | 24 ++++++- .../fixtures/auto_layout_mailer/hello.html.erb | 1 + .../explicit_layout_mailer/logout.html.erb | 1 + .../explicit_layout_mailer/signup.html.erb | 1 + .../fixtures/layouts/auto_layout_mailer.html.erb | 1 + actionmailer/test/fixtures/layouts/spam.html.erb | 1 + actionmailer/test/mail_layout_test.rb | 78 ++++++++++++++++++++++ 8 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb create mode 100644 actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb create mode 100644 actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb create mode 100644 actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb create mode 100644 actionmailer/test/fixtures/layouts/spam.html.erb create mode 100644 actionmailer/test/mail_layout_test.rb (limited to 'actionmailer') diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG index bdae0d4d3d..fc02ae8ffc 100644 --- a/actionmailer/CHANGELOG +++ b/actionmailer/CHANGELOG @@ -1,3 +1,8 @@ +* Add layout functionality to mailers [Pratik] + + Mailer layouts behaves just like controller layouts, except layout names need to + have '_mailer' postfix for them to be automatically picked up. + *2.1.0 (May 31st, 2008)* * Fixed that a return-path header would be ignored #7572 [joost] diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 5b3c560390..96e514e0db 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -246,7 +246,10 @@ module ActionMailer #:nodoc: # +implicit_parts_order+. class Base include AdvAttrAccessor, PartContainer - include ActionController::UrlWriter if Object.const_defined?(:ActionController) + if Object.const_defined?(:ActionController) + include ActionController::UrlWriter + include ActionController::Layout + end private_class_method :new #:nodoc: @@ -362,6 +365,7 @@ module ActionMailer #:nodoc: # The mail object instance referenced by this mailer. attr_reader :mail + attr_reader :template_name, :default_template_name, :action_name class << self attr_writer :mailer_name @@ -530,6 +534,7 @@ module ActionMailer #:nodoc: @content_type ||= @@default_content_type.dup @implicit_parts_order ||= @@default_implicit_parts_order.dup @template ||= method_name + @default_template_name = @action_name = @template @mailer_name ||= self.class.name.underscore @parts ||= [] @headers ||= {} @@ -546,7 +551,22 @@ module ActionMailer #:nodoc: if opts[:file] && (opts[:file] !~ /\// && !opts[:file].respond_to?(:render)) opts[:file] = "#{mailer_name}/#{opts[:file]}" end - initialize_template_class(body).render(opts) + + begin + old_template, @template = @template, initialize_template_class(body) + layout = respond_to?(:pick_layout, true) ? pick_layout(opts) : false + @template.render(opts.merge(:layout => layout)) + ensure + @template = old_template + end + end + + def default_template_format + :html + end + + def candidate_for_layout?(options) + !@template.send(:_exempt_from_layout?, default_template_name) end def template_root diff --git a/actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb b/actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb new file mode 100644 index 0000000000..54950788f7 --- /dev/null +++ b/actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb @@ -0,0 +1 @@ +Inside \ No newline at end of file diff --git a/actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb b/actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb new file mode 100644 index 0000000000..0533a3b2fe --- /dev/null +++ b/actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb @@ -0,0 +1 @@ +You logged out \ No newline at end of file diff --git a/actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb b/actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb new file mode 100644 index 0000000000..4789e888c6 --- /dev/null +++ b/actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb @@ -0,0 +1 @@ +We do not spam \ No newline at end of file diff --git a/actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb b/actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb new file mode 100644 index 0000000000..932271450c --- /dev/null +++ b/actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb @@ -0,0 +1 @@ +Hello from layout <%= yield %> \ No newline at end of file diff --git a/actionmailer/test/fixtures/layouts/spam.html.erb b/actionmailer/test/fixtures/layouts/spam.html.erb new file mode 100644 index 0000000000..619d6b16b4 --- /dev/null +++ b/actionmailer/test/fixtures/layouts/spam.html.erb @@ -0,0 +1 @@ +Spammer layout <%= yield %> \ No newline at end of file diff --git a/actionmailer/test/mail_layout_test.rb b/actionmailer/test/mail_layout_test.rb new file mode 100644 index 0000000000..ffba9a16bd --- /dev/null +++ b/actionmailer/test/mail_layout_test.rb @@ -0,0 +1,78 @@ +require 'abstract_unit' + +class AutoLayoutMailer < ActionMailer::Base + def hello(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + end + + def spam(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + body render(:inline => "Hello, <%= @world %>", :layout => 'spam', :body => { :world => "Earth" }) + end + + def nolayout(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + body render(:inline => "Hello, <%= @world %>", :layout => false, :body => { :world => "Earth" }) + end +end + +class ExplicitLayoutMailer < ActionMailer::Base + layout 'spam', :except => [:logout] + + def signup(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + end + + def logout(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + end +end + +class LayoutMailerTest < Test::Unit::TestCase + def setup + set_delivery_method :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries = [] + + @recipient = 'test@localhost' + end + + def teardown + restore_delivery_method + end + + def test_should_pickup_default_layout + mail = AutoLayoutMailer.create_hello(@recipient) + assert_equal "Hello from layout Inside", mail.body.strip + end + + def test_should_pickup_layout_given_to_render + mail = AutoLayoutMailer.create_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_explicit_class_layout + mail = ExplicitLayoutMailer.create_signup(@recipient) + assert_equal "Spammer layout We do not spam", mail.body.strip + end + + def test_explicit_layout_exceptions + mail = ExplicitLayoutMailer.create_logout(@recipient) + assert_equal "You logged out", mail.body.strip + end +end -- cgit v1.2.3 From a1eb4e11c2cccb91483fa15f1a1a0b2ae518d2cf Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 31 Aug 2008 13:15:26 -0700 Subject: Get rid of 'Object#send!'. It was originally added because it's in Ruby 1.9, but it has since been removed from 1.9. Signed-off-by: Jeremy Kemper Conflicts: actionpack/test/controller/layout_test.rb --- actionmailer/lib/action_mailer/helpers.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionmailer') diff --git a/actionmailer/lib/action_mailer/helpers.rb b/actionmailer/lib/action_mailer/helpers.rb index 9c5fcc6afb..5f6dcd77cd 100644 --- a/actionmailer/lib/action_mailer/helpers.rb +++ b/actionmailer/lib/action_mailer/helpers.rb @@ -72,7 +72,7 @@ module ActionMailer methods.flatten.each do |method| master_helper_module.module_eval <<-end_eval def #{method}(*args, &block) - controller.send!(%(#{method}), *args, &block) + controller.__send__(%(#{method}), *args, &block) end end_eval end @@ -92,7 +92,7 @@ module ActionMailer inherited_without_helper(child) begin child.master_helper_module = Module.new - child.master_helper_module.send! :include, master_helper_module + child.master_helper_module.__send__(:include, master_helper_module) child.helper child.name.to_s.underscore rescue MissingSourceFile => e raise unless e.is_missing?("helpers/#{child.name.to_s.underscore}_helper") -- cgit v1.2.3 From 36c6aa01ee0a7aee5b0510a8e649c44de318b060 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 9 Sep 2008 17:20:55 -0500 Subject: Revert "Add layout functionality to mailers." This reverts commit e9a8e0053be3b293ab89fb584f1d660063f107aa. --- actionmailer/CHANGELOG | 5 -- actionmailer/lib/action_mailer/base.rb | 24 +------ .../fixtures/auto_layout_mailer/hello.html.erb | 1 - .../explicit_layout_mailer/logout.html.erb | 1 - .../explicit_layout_mailer/signup.html.erb | 1 - .../fixtures/layouts/auto_layout_mailer.html.erb | 1 - actionmailer/test/fixtures/layouts/spam.html.erb | 1 - actionmailer/test/mail_layout_test.rb | 78 ---------------------- 8 files changed, 2 insertions(+), 110 deletions(-) delete mode 100644 actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb delete mode 100644 actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb delete mode 100644 actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb delete mode 100644 actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb delete mode 100644 actionmailer/test/fixtures/layouts/spam.html.erb delete mode 100644 actionmailer/test/mail_layout_test.rb (limited to 'actionmailer') diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG index fc02ae8ffc..bdae0d4d3d 100644 --- a/actionmailer/CHANGELOG +++ b/actionmailer/CHANGELOG @@ -1,8 +1,3 @@ -* Add layout functionality to mailers [Pratik] - - Mailer layouts behaves just like controller layouts, except layout names need to - have '_mailer' postfix for them to be automatically picked up. - *2.1.0 (May 31st, 2008)* * Fixed that a return-path header would be ignored #7572 [joost] diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 96e514e0db..5b3c560390 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -246,10 +246,7 @@ module ActionMailer #:nodoc: # +implicit_parts_order+. class Base include AdvAttrAccessor, PartContainer - if Object.const_defined?(:ActionController) - include ActionController::UrlWriter - include ActionController::Layout - end + include ActionController::UrlWriter if Object.const_defined?(:ActionController) private_class_method :new #:nodoc: @@ -365,7 +362,6 @@ module ActionMailer #:nodoc: # The mail object instance referenced by this mailer. attr_reader :mail - attr_reader :template_name, :default_template_name, :action_name class << self attr_writer :mailer_name @@ -534,7 +530,6 @@ module ActionMailer #:nodoc: @content_type ||= @@default_content_type.dup @implicit_parts_order ||= @@default_implicit_parts_order.dup @template ||= method_name - @default_template_name = @action_name = @template @mailer_name ||= self.class.name.underscore @parts ||= [] @headers ||= {} @@ -551,22 +546,7 @@ module ActionMailer #:nodoc: if opts[:file] && (opts[:file] !~ /\// && !opts[:file].respond_to?(:render)) opts[:file] = "#{mailer_name}/#{opts[:file]}" end - - begin - old_template, @template = @template, initialize_template_class(body) - layout = respond_to?(:pick_layout, true) ? pick_layout(opts) : false - @template.render(opts.merge(:layout => layout)) - ensure - @template = old_template - end - end - - def default_template_format - :html - end - - def candidate_for_layout?(options) - !@template.send(:_exempt_from_layout?, default_template_name) + initialize_template_class(body).render(opts) end def template_root diff --git a/actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb b/actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb deleted file mode 100644 index 54950788f7..0000000000 --- a/actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb +++ /dev/null @@ -1 +0,0 @@ -Inside \ No newline at end of file diff --git a/actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb b/actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb deleted file mode 100644 index 0533a3b2fe..0000000000 --- a/actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb +++ /dev/null @@ -1 +0,0 @@ -You logged out \ No newline at end of file diff --git a/actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb b/actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb deleted file mode 100644 index 4789e888c6..0000000000 --- a/actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb +++ /dev/null @@ -1 +0,0 @@ -We do not spam \ No newline at end of file diff --git a/actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb b/actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb deleted file mode 100644 index 932271450c..0000000000 --- a/actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb +++ /dev/null @@ -1 +0,0 @@ -Hello from layout <%= yield %> \ No newline at end of file diff --git a/actionmailer/test/fixtures/layouts/spam.html.erb b/actionmailer/test/fixtures/layouts/spam.html.erb deleted file mode 100644 index 619d6b16b4..0000000000 --- a/actionmailer/test/fixtures/layouts/spam.html.erb +++ /dev/null @@ -1 +0,0 @@ -Spammer layout <%= yield %> \ No newline at end of file diff --git a/actionmailer/test/mail_layout_test.rb b/actionmailer/test/mail_layout_test.rb deleted file mode 100644 index ffba9a16bd..0000000000 --- a/actionmailer/test/mail_layout_test.rb +++ /dev/null @@ -1,78 +0,0 @@ -require 'abstract_unit' - -class AutoLayoutMailer < ActionMailer::Base - def hello(recipient) - recipients recipient - subject "You have a mail" - from "tester@example.com" - end - - def spam(recipient) - recipients recipient - subject "You have a mail" - from "tester@example.com" - body render(:inline => "Hello, <%= @world %>", :layout => 'spam', :body => { :world => "Earth" }) - end - - def nolayout(recipient) - recipients recipient - subject "You have a mail" - from "tester@example.com" - body render(:inline => "Hello, <%= @world %>", :layout => false, :body => { :world => "Earth" }) - end -end - -class ExplicitLayoutMailer < ActionMailer::Base - layout 'spam', :except => [:logout] - - def signup(recipient) - recipients recipient - subject "You have a mail" - from "tester@example.com" - end - - def logout(recipient) - recipients recipient - subject "You have a mail" - from "tester@example.com" - end -end - -class LayoutMailerTest < Test::Unit::TestCase - def setup - set_delivery_method :test - ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries = [] - - @recipient = 'test@localhost' - end - - def teardown - restore_delivery_method - end - - def test_should_pickup_default_layout - mail = AutoLayoutMailer.create_hello(@recipient) - assert_equal "Hello from layout Inside", mail.body.strip - end - - def test_should_pickup_layout_given_to_render - mail = AutoLayoutMailer.create_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_explicit_class_layout - mail = ExplicitLayoutMailer.create_signup(@recipient) - assert_equal "Spammer layout We do not spam", mail.body.strip - end - - def test_explicit_layout_exceptions - mail = ExplicitLayoutMailer.create_logout(@recipient) - assert_equal "You logged out", mail.body.strip - end -end -- cgit v1.2.3 From 6228220c9b8a3bb32f8617ad2d963dabc965376e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 9 Sep 2008 17:25:09 -0500 Subject: Revert "Revert "Add layout functionality to mailers."" This reverts commit 36c6aa01ee0a7aee5b0510a8e649c44de318b060. --- actionmailer/CHANGELOG | 5 ++ actionmailer/lib/action_mailer/base.rb | 24 ++++++- .../fixtures/auto_layout_mailer/hello.html.erb | 1 + .../explicit_layout_mailer/logout.html.erb | 1 + .../explicit_layout_mailer/signup.html.erb | 1 + .../fixtures/layouts/auto_layout_mailer.html.erb | 1 + actionmailer/test/fixtures/layouts/spam.html.erb | 1 + actionmailer/test/mail_layout_test.rb | 78 ++++++++++++++++++++++ 8 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb create mode 100644 actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb create mode 100644 actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb create mode 100644 actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb create mode 100644 actionmailer/test/fixtures/layouts/spam.html.erb create mode 100644 actionmailer/test/mail_layout_test.rb (limited to 'actionmailer') diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG index bdae0d4d3d..fc02ae8ffc 100644 --- a/actionmailer/CHANGELOG +++ b/actionmailer/CHANGELOG @@ -1,3 +1,8 @@ +* Add layout functionality to mailers [Pratik] + + Mailer layouts behaves just like controller layouts, except layout names need to + have '_mailer' postfix for them to be automatically picked up. + *2.1.0 (May 31st, 2008)* * Fixed that a return-path header would be ignored #7572 [joost] diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 5b3c560390..96e514e0db 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -246,7 +246,10 @@ module ActionMailer #:nodoc: # +implicit_parts_order+. class Base include AdvAttrAccessor, PartContainer - include ActionController::UrlWriter if Object.const_defined?(:ActionController) + if Object.const_defined?(:ActionController) + include ActionController::UrlWriter + include ActionController::Layout + end private_class_method :new #:nodoc: @@ -362,6 +365,7 @@ module ActionMailer #:nodoc: # The mail object instance referenced by this mailer. attr_reader :mail + attr_reader :template_name, :default_template_name, :action_name class << self attr_writer :mailer_name @@ -530,6 +534,7 @@ module ActionMailer #:nodoc: @content_type ||= @@default_content_type.dup @implicit_parts_order ||= @@default_implicit_parts_order.dup @template ||= method_name + @default_template_name = @action_name = @template @mailer_name ||= self.class.name.underscore @parts ||= [] @headers ||= {} @@ -546,7 +551,22 @@ module ActionMailer #:nodoc: if opts[:file] && (opts[:file] !~ /\// && !opts[:file].respond_to?(:render)) opts[:file] = "#{mailer_name}/#{opts[:file]}" end - initialize_template_class(body).render(opts) + + begin + old_template, @template = @template, initialize_template_class(body) + layout = respond_to?(:pick_layout, true) ? pick_layout(opts) : false + @template.render(opts.merge(:layout => layout)) + ensure + @template = old_template + end + end + + def default_template_format + :html + end + + def candidate_for_layout?(options) + !@template.send(:_exempt_from_layout?, default_template_name) end def template_root diff --git a/actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb b/actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb new file mode 100644 index 0000000000..54950788f7 --- /dev/null +++ b/actionmailer/test/fixtures/auto_layout_mailer/hello.html.erb @@ -0,0 +1 @@ +Inside \ No newline at end of file diff --git a/actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb b/actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb new file mode 100644 index 0000000000..0533a3b2fe --- /dev/null +++ b/actionmailer/test/fixtures/explicit_layout_mailer/logout.html.erb @@ -0,0 +1 @@ +You logged out \ No newline at end of file diff --git a/actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb b/actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb new file mode 100644 index 0000000000..4789e888c6 --- /dev/null +++ b/actionmailer/test/fixtures/explicit_layout_mailer/signup.html.erb @@ -0,0 +1 @@ +We do not spam \ No newline at end of file diff --git a/actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb b/actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb new file mode 100644 index 0000000000..932271450c --- /dev/null +++ b/actionmailer/test/fixtures/layouts/auto_layout_mailer.html.erb @@ -0,0 +1 @@ +Hello from layout <%= yield %> \ No newline at end of file diff --git a/actionmailer/test/fixtures/layouts/spam.html.erb b/actionmailer/test/fixtures/layouts/spam.html.erb new file mode 100644 index 0000000000..619d6b16b4 --- /dev/null +++ b/actionmailer/test/fixtures/layouts/spam.html.erb @@ -0,0 +1 @@ +Spammer layout <%= yield %> \ No newline at end of file diff --git a/actionmailer/test/mail_layout_test.rb b/actionmailer/test/mail_layout_test.rb new file mode 100644 index 0000000000..ffba9a16bd --- /dev/null +++ b/actionmailer/test/mail_layout_test.rb @@ -0,0 +1,78 @@ +require 'abstract_unit' + +class AutoLayoutMailer < ActionMailer::Base + def hello(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + end + + def spam(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + body render(:inline => "Hello, <%= @world %>", :layout => 'spam', :body => { :world => "Earth" }) + end + + def nolayout(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + body render(:inline => "Hello, <%= @world %>", :layout => false, :body => { :world => "Earth" }) + end +end + +class ExplicitLayoutMailer < ActionMailer::Base + layout 'spam', :except => [:logout] + + def signup(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + end + + def logout(recipient) + recipients recipient + subject "You have a mail" + from "tester@example.com" + end +end + +class LayoutMailerTest < Test::Unit::TestCase + def setup + set_delivery_method :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries = [] + + @recipient = 'test@localhost' + end + + def teardown + restore_delivery_method + end + + def test_should_pickup_default_layout + mail = AutoLayoutMailer.create_hello(@recipient) + assert_equal "Hello from layout Inside", mail.body.strip + end + + def test_should_pickup_layout_given_to_render + mail = AutoLayoutMailer.create_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_explicit_class_layout + mail = ExplicitLayoutMailer.create_signup(@recipient) + assert_equal "Spammer layout We do not spam", mail.body.strip + end + + def test_explicit_layout_exceptions + mail = ExplicitLayoutMailer.create_logout(@recipient) + assert_equal "You logged out", mail.body.strip + end +end -- cgit v1.2.3 From f3f7d166d8e7a1a2e15371f2870115406e1aaac2 Mon Sep 17 00:00:00 2001 From: "U-ESCAPEE\\Mack" Date: Thu, 4 Sep 2008 12:03:29 -0400 Subject: Fixed problem causes by leftover backup templates ending in tilde [state:committed #969] Signed-off-by: David Heinemeier Hansson --- actionmailer/lib/action_mailer/base.rb | 2 +- .../test_mailer/implicitly_multipart_example.text.html.erb~ | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb~ (limited to 'actionmailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 96e514e0db..bfe435550b 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -466,7 +466,7 @@ module ActionMailer #:nodoc: template = template_root["#{mailer_name}/#{File.basename(path)}"] # Skip unless template has a multipart format - next unless template.multipart? + next unless template && template.multipart? @parts << Part.new( :content_type => template.content_type, diff --git a/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb~ b/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb~ new file mode 100644 index 0000000000..946d99ede5 --- /dev/null +++ b/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb~ @@ -0,0 +1,10 @@ + + + HTML formatted message to <%= @recipient %>. + + + + + HTML formatted message to <%= @recipient %>. + + -- cgit v1.2.3