Refactor content type setting, added tests to ensure boundary exists on multipart and fixed typo

上级 1b3cb54e
......@@ -49,7 +49,7 @@ module ActionMailer #:nodoc:
#
# The mail method, if not passed a block, will inspect your views and send all the views with
# the same name as the method, so the above action would send the +welcome.plain.erb+ view file
# as well as the +welcome.html.erb+ view file in a +multipart/alternate+ email.
# as well as the +welcome.html.erb+ view file in a +multipart/alternative+ email.
#
# If you want to explicitly render only certain templates, pass a block:
#
......@@ -162,7 +162,7 @@ module ActionMailer #:nodoc:
#
# Which will (if it had both a <tt>.text.erb</tt> and <tt>.html.erb</tt> tempalte in the view
# directory), send a complete <tt>multipart/mixed</tt> email with two parts, the first part being
# a <tt>multipart/alternate</tt> with the text and HTML email parts inside, and the second being
# a <tt>multipart/alternative</tt> with the text and HTML email parts inside, and the second being
# a <tt>application/pdf</tt> with a Base64 encoded copy of the file.pdf book with the filename
# +free_book.pdf+.
#
......@@ -445,7 +445,7 @@ def attachments
# format.html { render :text => "<h1>Hello Mikel!</h1>" }
# end
#
# Which will render a <tt>multipart/alternate</tt> email with <tt>text/plain</tt> and
# Which will render a <tt>multipart/alternative</tt> email with <tt>text/plain</tt> and
# <tt>text/html</tt> parts.
def mail(headers={}, &block)
# Guard flag to prevent both the old and the new API from firing
......@@ -465,10 +465,11 @@ def mail(headers={}, &block)
# Render the templates and blocks
responses, sort_order = collect_responses_and_sort_order(headers, &block)
content_type ||= create_parts_from_responses(m, responses, charset)
create_parts_from_responses(m, responses, charset)
# Tidy up content type, charset, mime version and sort order
m.content_type = content_type
m.content_type = set_content_type(m, content_type)
m.charset = charset
m.mime_version = mime_version
sort_order = headers[:parts_order] || sort_order || self.class.default_implicit_parts_order.dup
......@@ -485,6 +486,20 @@ def mail(headers={}, &block)
protected
def set_content_type(m, user_content_type)
params = m.content_type_parameters || {}
case
when user_content_type.present?
user_content_type
when m.has_attachments?
["multipart", "mixed", params]
when m.multipart?
["multipart", "alternative", params]
else
self.class.default_content_type.dup
end
end
def default_subject #:nodoc:
mailer_scope = self.class.mailer_name.gsub('/', '.')
I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize)
......@@ -543,16 +558,14 @@ def create_parts_from_responses(m, responses, charset) #:nodoc:
if responses.size == 1 && !m.has_attachments?
m.body = responses[0][:body]
return responses[0][:content_type]
elsif responses.size > 1 && m.has_attachments?
elsif responses.size > 1 && m.has_attachments?
container = Mail::Part.new
container.content_type = "multipart/alternate"
container.content_type = "multipart/alternative"
responses.each { |r| insert_part(container, r, charset) }
m.add_part(container)
else
responses.each { |r| insert_part(m, r, charset) }
end
m.has_attachments? ? "multipart/mixed" : "multipart/alternate"
end
def insert_part(container, response, charset) #:nodoc:
......
......@@ -200,7 +200,7 @@ def explicit_multipart_with_any(hash = {})
test "implicit multipart" do
email = BaseMailer.implicit_multipart.deliver
assert_equal(2, email.parts.size)
assert_equal("multipart/alternate", email.mime_type)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("TEXT Implicit Multipart", email.parts[0].body.encoded)
assert_equal("text/html", email.parts[1].mime_type)
......@@ -223,7 +223,7 @@ def explicit_multipart_with_any(hash = {})
test "implicit multipart with attachments creates nested parts" do
email = BaseMailer.implicit_multipart(:attachments => true).deliver
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternate", email.parts[1].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
assert_equal("TEXT Implicit Multipart", email.parts[1].parts[0].body.encoded)
assert_equal("text/html", email.parts[1].parts[1].mime_type)
......@@ -235,7 +235,7 @@ def explicit_multipart_with_any(hash = {})
swap BaseMailer, :default_implicit_parts_order => order do
email = BaseMailer.implicit_multipart(:attachments => true).deliver
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternate", email.parts[1].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[1].mime_type)
assert_equal("text/html", email.parts[1].parts[0].mime_type)
end
......@@ -244,7 +244,7 @@ def explicit_multipart_with_any(hash = {})
test "implicit multipart with default locale" do
email = BaseMailer.implicit_with_locale.deliver
assert_equal(2, email.parts.size)
assert_equal("multipart/alternate", email.mime_type)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("Implicit with locale TEXT", email.parts[0].body.encoded)
assert_equal("text/html", email.parts[1].mime_type)
......@@ -255,7 +255,7 @@ def explicit_multipart_with_any(hash = {})
swap I18n, :locale => :pl do
email = BaseMailer.implicit_with_locale.deliver
assert_equal(2, email.parts.size)
assert_equal("multipart/alternate", email.mime_type)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("Implicit with locale PL TEXT", email.parts[0].body.encoded)
assert_equal("text/html", email.parts[1].mime_type)
......@@ -287,7 +287,7 @@ def explicit_multipart_with_any(hash = {})
test "explicit multipart" do
email = BaseMailer.explicit_multipart.deliver
assert_equal(2, email.parts.size)
assert_equal("multipart/alternate", email.mime_type)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("TEXT Explicit Multipart", email.parts[0].body.encoded)
assert_equal("text/html", email.parts[1].mime_type)
......@@ -310,7 +310,7 @@ def explicit_multipart_with_any(hash = {})
test "explicit multipart with attachments creates nested parts" do
email = BaseMailer.explicit_multipart(:attachments => true).deliver
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternate", email.parts[1].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
assert_equal("TEXT Explicit Multipart", email.parts[1].parts[0].body.encoded)
assert_equal("text/html", email.parts[1].parts[1].mime_type)
......@@ -320,7 +320,7 @@ def explicit_multipart_with_any(hash = {})
test "explicit multipart with templates" do
email = BaseMailer.explicit_multipart_templates.deliver
assert_equal(2, email.parts.size)
assert_equal("multipart/alternate", email.mime_type)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/html", email.parts[0].mime_type)
assert_equal("HTML Explicit Multipart Templates", email.parts[0].body.encoded)
assert_equal("text/plain", email.parts[1].mime_type)
......@@ -330,7 +330,7 @@ def explicit_multipart_with_any(hash = {})
test "explicit multipart with any" do
email = BaseMailer.explicit_multipart_with_any.deliver
assert_equal(2, email.parts.size)
assert_equal("multipart/alternate", email.mime_type)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("Format with any!", email.parts[0].body.encoded)
assert_equal("text/html", email.parts[1].mime_type)
......@@ -370,6 +370,11 @@ def explicit_multipart_with_any(hash = {})
BaseMailer.expects(:welcome).returns(mail)
BaseMailer.welcome.deliver
end
test "explicit multipart should be multipart" do
mail = BaseMailer.explicit_multipart
assert_not_nil(mail.content_type_parameters[:boundary])
end
protected
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册