From bd5ed651105663cb4bc5acd86ad8bdf48251d0fe Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Mon, 26 Nov 2007 03:36:28 +0000 Subject: [PATCH] Update ActionMailer so it treats ActionView the same way that ActionController does. Closes #10244 [rick] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8212 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionmailer/CHANGELOG | 5 +++ actionmailer/lib/action_mailer/base.rb | 35 +++++++++++++++---- ....plain.erb => _subtemplate.text.plain.erb} | 0 .../included_subtemplate.text.plain.erb | 2 +- actionmailer/test/mail_render_test.rb | 13 +++++++ 5 files changed, 48 insertions(+), 7 deletions(-) rename actionmailer/test/fixtures/test_mailer/{subtemplate.text.plain.erb => _subtemplate.text.plain.erb} (100%) diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG index 09ca77ac178d0..92dc24726bf2f 100644 --- a/actionmailer/CHANGELOG +++ b/actionmailer/CHANGELOG @@ -1,5 +1,10 @@ *SVN* +* Update ActionMailer so it treats ActionView the same way that ActionController does. Closes #10244 [rick] + + * Pass the template_root as an array as ActionView's view_path + * Request templates with the "#{mailer_name}/#{action}" as opposed to just "#{action}" + * Fixed that partials would be broken when using text.plain.erb as the extension #10130 [java] * Update README to use new smtp settings configuration API. Closes #10060 [psq] diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 8bf051c99f877..b15c010e83cd3 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -298,11 +298,6 @@ class Base # This defaults to the value for the +default_implicit_parts_order+. adv_attr_accessor :implicit_parts_order - # 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. - adv_attr_accessor :mailer_name - # Defaults to "1.0", but may be explicitly given if needed. adv_attr_accessor :mime_version @@ -322,10 +317,35 @@ class Base # have multiple mailer methods share the same template. adv_attr_accessor :template + # 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. + def mailer_name(value = nil) + if value + self.mailer_name = value + else + self.class.mailer_name + end + end + + def mailer_name=(value) + self.class.mailer_name = value + end + # The mail object instance referenced by this mailer. attr_reader :mail class << self + attr_writer :mailer_name + + def mailer_name + @mailer_name ||= name.underscore + end + + # for ActionView compatibility + 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 @@ -476,6 +496,9 @@ def render_message(method_name, body) def render(opts) body = opts.delete(:body) + if opts[:file] && opts[:file] !~ /\// + opts[:file] = "#{mailer_name}/#{opts[:file]}" + end initialize_template_class(body).render(opts) end @@ -484,7 +507,7 @@ def template_path end def initialize_template_class(assigns) - ActionView::Base.new(template_path, assigns, self) + ActionView::Base.new([template_root], assigns, self) end def sort_parts(parts, order = []) diff --git a/actionmailer/test/fixtures/test_mailer/subtemplate.text.plain.erb b/actionmailer/test/fixtures/test_mailer/_subtemplate.text.plain.erb similarity index 100% rename from actionmailer/test/fixtures/test_mailer/subtemplate.text.plain.erb rename to actionmailer/test/fixtures/test_mailer/_subtemplate.text.plain.erb diff --git a/actionmailer/test/fixtures/test_mailer/included_subtemplate.text.plain.erb b/actionmailer/test/fixtures/test_mailer/included_subtemplate.text.plain.erb index de9b2039fadc7..a93c30ea1a732 100644 --- a/actionmailer/test/fixtures/test_mailer/included_subtemplate.text.plain.erb +++ b/actionmailer/test/fixtures/test_mailer/included_subtemplate.text.plain.erb @@ -1 +1 @@ -Hey Ho, <%= render "subtemplate" %> \ No newline at end of file +Hey Ho, <%= render :partial => "subtemplate" %> \ No newline at end of file diff --git a/actionmailer/test/mail_render_test.rb b/actionmailer/test/mail_render_test.rb index 0bd5eafd59166..475735e7b09c8 100644 --- a/actionmailer/test/mail_render_test.rb +++ b/actionmailer/test/mail_render_test.rb @@ -26,6 +26,13 @@ def included_subtemplate(recipient) subject "Including another template in the one being rendered" from "tester@example.com" end + + def included_old_subtemplate(recipient) + recipients recipient + subject "Including another template in the one being rendered" + from "tester@example.com" + body render(:inline => "Hello, <%= render \"subtemplate\" %>", :body => { :world => "Earth" }) + end def initialize_defaults(method_name) super @@ -81,6 +88,12 @@ def test_included_subtemplate mail = RenderMailer.deliver_included_subtemplate(@recipient) assert_equal "Hey Ho, let's go!", mail.body.strip end + + def test_deprecated_old_subtemplate + assert_raises ActionView::ActionViewError do + RenderMailer.deliver_included_old_subtemplate(@recipient) + end + end end class FirstSecondHelperTest < Test::Unit::TestCase