提交 b2504f8b 编写于 作者: J Joshua Peek

Tidy up ActionMailer rendering logic to take advantage of view path cache...

Tidy up ActionMailer rendering logic to take advantage of view path cache instead of using file system lookups
上级 ed8a882e
......@@ -253,9 +253,6 @@ class Base
class_inheritable_accessor :view_paths
cattr_accessor :logger
cattr_accessor :template_extensions
@@template_extensions = ['erb', 'builder', 'rhtml', 'rxml']
@@smtp_settings = {
:address => "localhost",
:port => 25,
......@@ -414,15 +411,10 @@ def deliver(mail)
new.deliver!(mail)
end
# Register a template extension so mailer templates written in a
# templating language other than rhtml or rxml are supported.
# To use this, include in your template-language plugin's init
# code or on a per-application basis, this can be invoked from
# <tt>config/environment.rb</tt>:
#
# ActionMailer::Base.register_template_extension('haml')
def register_template_extension(extension)
template_extensions << extension
ActiveSupport::Deprecation.warn(
"ActionMailer::Base.register_template_extension has been deprecated." +
"Use ActionView::Base.register_template_extension instead", caller)
end
def template_root
......@@ -455,16 +447,18 @@ def create!(method_name, *parameters) #:nodoc:
# "the_template_file.text.html.erb", etc.). Only do this if parts
# have not already been specified manually.
if @parts.empty?
templates = Dir.glob("#{template_path}/#{@template}.*")
templates.each do |path|
basename = File.basename(path)
template_regex = Regexp.new("^([^\\\.]+)\\\.([^\\\.]+\\\.[^\\\.]+)\\\.(" + template_extensions.join('|') + ")$")
next unless md = template_regex.match(basename)
template_name = basename
content_type = md.captures[1].gsub('.', '/')
@parts << Part.new(:content_type => content_type,
:disposition => "inline", :charset => charset,
:body => render_message(template_name, @body))
Dir.glob("#{template_path}/#{@template}.*").each do |path|
template = template_root["#{mailer_name}/#{File.basename(path)}"]
# Skip unless template has a multipart format
next unless template.multipart?
@parts << Part.new(
:content_type => template.content_type,
:disposition => "inline",
:charset => charset,
:body => render_message(template, @body)
)
end
unless @parts.empty?
@content_type = "multipart/alternative"
......@@ -476,9 +470,8 @@ def create!(method_name, *parameters) #:nodoc:
# also render a "normal" template (without the content type). If a
# normal template exists (or if there were no implicit parts) we render
# it.
template_exists = @parts.empty?
template_exists ||= Dir.glob("#{template_path}/#{@template}.*").any? { |i| File.basename(i).split(".").length == 2 }
@body = render_message(@template, @body) if template_exists
template = template_root["#{mailer_name}/#{@template}"]
@body = render_message(@template, @body) if template
# Finally, if there are other message parts and a textual body exists,
# we shift it onto the front of the parts and set the body to nil (so
......@@ -538,7 +531,7 @@ def render_message(method_name, body)
def render(opts)
body = opts.delete(:body)
if opts[:file] && opts[:file] !~ /\//
if opts[:file] && (opts[:file] !~ /\// && !opts[:file].respond_to?(:render))
opts[:file] = "#{mailer_name}/#{opts[:file]}"
end
initialize_template_class(body).render(opts)
......
......@@ -382,7 +382,6 @@ def test_custom_template
end
def test_custom_templating_extension
#
# N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
expected = new_mail
expected.to = @recipient
......@@ -394,14 +393,6 @@ def test_custom_templating_extension
# Stub the render method so no alternative renderers need be present.
ActionView::Base.any_instance.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}")
# If the template is not registered, there should be no parts.
created = nil
assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
assert_not_nil created
assert_equal 0, created.parts.length
ActionMailer::Base.register_template_extension('haml')
# Now that the template is registered, there should be one part. The text/plain part.
created = nil
assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
......
......@@ -300,6 +300,8 @@ def file_exists?(template_path)
# # => 'users/legacy.rhtml'
#
def pick_template(template_path)
return template_path if template_path.respond_to?(:render)
path = template_path.sub(/^\//, '')
if m = path.match(/(.*)\.(\w+)$/)
template_file_name, template_file_extension = m[1], m[2]
......@@ -343,7 +345,8 @@ def render_file(template_path, use_full_path = nil, local_assigns = {}) #:nodoc:
ActiveSupport::Deprecation.warn("use_full_path option has been deprecated and has no affect.", caller)
end
if defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) && !template_path.include?("/")
if defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) &&
template_path.is_a?(String) && !template_path.include?("/")
raise ActionViewError, <<-END_ERROR
Due to changes in ActionMailer, you need to provide the mailer_name along with the template name.
......
......@@ -22,6 +22,14 @@ def format_and_extension
end
memoize :format_and_extension
def multipart?
format && format.include?('.')
end
def content_type
format.gsub('.', '/')
end
def mime_type
Mime::Type.lookup_by_extension(format) if format
end
......@@ -84,7 +92,7 @@ def find_full_path(path, load_paths)
# [base_path, name, format, extension]
def split(file)
if m = file.match(/^(.*\/)?([^\.]+)\.?(\w+)?\.?(\w+)?\.?(\w+)?$/)
if m[5] # Mulipart formats
if m[5] # Multipart formats
[m[1], m[2], "#{m[3]}.#{m[4]}", m[5]]
elsif m[4] # Single format
[m[1], m[2], m[3], m[4]]
......
......@@ -17,6 +17,8 @@
end
end
ActionMailer::Base.template_root = FIXTURE_LOAD_PATH
class AssertSelectTest < Test::Unit::TestCase
class AssertSelectController < ActionController::Base
def response_with=(content)
......@@ -69,7 +71,6 @@ def setup
ActionMailer::Base.deliveries = []
end
def teardown
ActionMailer::Base.deliveries.clear
end
......@@ -91,7 +92,6 @@ def test_assert_select
assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_select "p" }
end
def test_equality_true_false
render_html %Q{<div id="1"></div><div id="2"></div>}
assert_nothing_raised { assert_select "div" }
......@@ -102,7 +102,6 @@ def test_equality_true_false
assert_nothing_raised { assert_select "p", false }
end
def test_equality_string_and_regexp
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
assert_nothing_raised { assert_select "div", "foo" }
......@@ -116,7 +115,6 @@ def test_equality_string_and_regexp
assert_raises(AssertionFailedError) { assert_select "p", :text=>/foobar/ }
end
def test_equality_of_html
render_html %Q{<p>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</p>}
text = "\"This is not a big problem,\" he said."
......@@ -135,7 +133,6 @@ def test_equality_of_html
assert_raises(AssertionFailedError) { assert_select "pre", :html=>text }
end
def test_counts
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
assert_nothing_raised { assert_select "div", 2 }
......@@ -166,7 +163,6 @@ def test_counts
end
end
def test_substitution_values
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
assert_select "div#?", /\d+/ do |elements|
......@@ -181,7 +177,6 @@ def test_substitution_values
end
end
def test_nested_assert_select
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
assert_select "div" do |elements|
......@@ -208,7 +203,6 @@ def test_nested_assert_select
end
end
def test_assert_select_text_match
render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>}
assert_select "div" do
......@@ -225,7 +219,6 @@ def test_assert_select_text_match
end
end
# With single result.
def test_assert_select_from_rjs_with_single_result
render_rjs do |page|
......@@ -255,19 +248,16 @@ def test_assert_select_from_rjs_with_multiple_results
end
end
#
# Test css_select.
#
def test_css_select
render_html %Q{<div id="1"></div><div id="2"></div>}
assert 2, css_select("div").size
assert 0, css_select("p").size
end
def test_nested_css_select
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
assert_select "div#?", /\d+/ do |elements|
......@@ -286,7 +276,6 @@ def test_nested_css_select
end
end
# With one result.
def test_css_select_from_rjs_with_single_result
render_rjs do |page|
......@@ -309,12 +298,10 @@ def test_css_select_from_rjs_with_multiple_results
assert_equal 1, css_select("#2").size
end
#
# Test assert_select_rjs.
#
# Test that we can pick up all statements in the result.
def test_assert_select_rjs_picks_up_all_statements
render_rjs do |page|
......@@ -381,7 +368,6 @@ def test_assert_select_rjs_with_id
assert_raises(AssertionFailedError) { assert_select_rjs "test4" }
end
def test_assert_select_rjs_for_replace
render_rjs do |page|
page.replace "test1", "<div id=\"1\">foo</div>"
......@@ -600,7 +586,6 @@ def test_nested_assert_select_rjs_with_two_results
end
end
def test_feed_item_encoded
render_xml <<-EOF
<rss version="2.0">
......@@ -654,7 +639,6 @@ def test_feed_item_encoded
end
end
#
# Test assert_select_email
#
......@@ -670,7 +654,6 @@ def test_assert_select_email
end
end
protected
def render_html(html)
@controller.response_with = html
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册