Skip to content

Commit

Permalink
Added basic explicit multipart rendering and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim and Mikel Lindsaar committed Jan 23, 2010
1 parent 502028a commit c6b1626
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
1 change: 1 addition & 0 deletions actionmailer/lib/action_mailer.rb
Expand Up @@ -31,6 +31,7 @@ module ActionMailer
extend ::ActiveSupport::Autoload

autoload :AdvAttrAccessor
autoload :Collector
autoload :Base
autoload :DeliveryMethods
autoload :DeprecatedApi
Expand Down
28 changes: 21 additions & 7 deletions actionmailer/lib/action_mailer/base.rb
Expand Up @@ -297,8 +297,9 @@ class Base < AbstractController::Base
self.default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ]

# Expose the internal Mail message
# TODO: Make this an _internal ivar?
attr_reader :message

def headers(args=nil)
if args
ActiveSupport::Deprecation.warn "headers(Hash) is deprecated, please do headers[key] = value instead", caller
Expand Down Expand Up @@ -413,12 +414,25 @@ def mail(headers = {})
m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to]
m.date ||= headers[:date] if headers[:date]

if block_given?
# Do something
if headers[:body]
templates = [ActionView::Template::Text.new(headers[:body], format_for_text)]
elsif block_given?
collector = ActionMailer::Collector.new(self, {:charset => charset}) do
render action_name
end
yield collector

collector.responses.each do |response|
part = Mail::Part.new(response)
m.add_part(part)
end

else
# TODO Ensure that we don't need to pass I18n.locale as detail
templates = self.class.template_root.find_all(action_name, {}, self.class.mailer_name)

end

if templates
if templates.size == 1 && !m.has_attachments?
content_type ||= templates[0].mime_type.to_s
m.body = render_to_body(:_template => templates[0])
Expand All @@ -430,17 +444,17 @@ def mail(headers = {})
else
templates.each { |t| insert_part(m, t, charset) }
end

content_type ||= (m.has_attachments? ? "multipart/mixed" : "multipart/alternate")
end

content_type ||= (m.has_attachments? ? "multipart/mixed" : "multipart/alternate")

# Check if the content_type was not overwriten along the way and if so,
# fallback to default.
m.content_type = content_type || self.class.default_content_type.dup
m.charset = charset
m.mime_version = mime_version

unless m.parts.empty?
if m.parts.present? && templates
m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order.dup)
m.body.sort_parts!
end
Expand Down
2 changes: 1 addition & 1 deletion actionmailer/lib/action_mailer/mail_helper.rb
Expand Up @@ -18,7 +18,7 @@ def block_format(text)

# Access the mailer instance.
def mailer #:nodoc:
@controller
@_controller
end

# Access the message instance.
Expand Down
67 changes: 56 additions & 11 deletions actionmailer/test/base_test.rb
Expand Up @@ -69,6 +69,14 @@ def implicit_multipart(hash = {})
attachments['invoice.pdf'] = 'This is test File content' if hash.delete(:attachments)
mail(DEFAULT_HEADERS.merge(hash))
end

def explicit_multipart(hash = {})
mail(DEFAULT_HEADERS.merge(hash)) do |format|
format.text { render :text => "TEXT Explicit Multipart" }
format.html { render :text => "HTML Explicit Multipart" }
end
end

end

test "method call to mail does not raise error" do
Expand Down Expand Up @@ -106,6 +114,11 @@ def implicit_multipart(hash = {})
assert_equal("Welcome", email.body.encoded)
end

test "can pass in :body to the mail method hash" do
email = BaseMailer.deliver_welcome(:body => "Hello there")
assert_equal("Hello there", email.body.encoded)
end

# Custom headers
test "custom headers" do
email = BaseMailer.deliver_welcome
Expand Down Expand Up @@ -221,17 +234,49 @@ def implicit_multipart(hash = {})
assert_equal("HTML Implicit Multipart", email.parts[1].parts[1].body.encoded)
end

# TODO This should be fixed in mail. Sort parts should be recursive.
# test "implicit multipart with attachments and sort order" do
# order = ["text/html", "text/plain"]
# swap BaseMailer, :default_implicit_parts_order => order do
# email = BaseMailer.deliver_implicit_multipart(:attachments => true)
# assert_equal("application/pdf", email.parts[0].mime_type)
# assert_equal("multipart/alternate", 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
# end
test "implicit multipart with attachments and sort order" do
order = ["text/html", "text/plain"]
swap BaseMailer, :default_implicit_parts_order => order do
email = BaseMailer.deliver_implicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternate", 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
end

test "explicit multipart tests" do
email = BaseMailer.deliver_explicit_multipart
assert_equal(2, email.parts.size)
assert_equal("multipart/alternate", 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)
assert_equal("HTML Explicit Multipart", email.parts[1].body.encoded)
end

test "explicit multipart does not sort order" do
order = ["text/html", "text/plain"]
swap BaseMailer, :default_implicit_parts_order => order do
email = BaseMailer.deliver_explicit_multipart
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("text/html", email.parts[1].mime_type)

email = BaseMailer.deliver_explicit_multipart(:parts_order => order.reverse)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("text/html", email.parts[1].mime_type)
end
end

#test "explicit multipart with attachments creates nested parts" do
# email = BaseMailer.deliver_explicit_multipart(:attachments => true)
# assert_equal("application/pdf", email.parts[0].mime_type)
# assert_equal("multipart/alternate", 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)
# assert_equal("HTML Implicit Multipart", email.parts[1].parts[1].body.encoded)
#end

protected

Expand Down

0 comments on commit c6b1626

Please sign in to comment.