Skip to content
Browse files

Added basic explicit multipart rendering and tests

  • Loading branch information...
1 parent 502028a commit c6b16260fe3d1435848e78415bd0b40c10ad7424 José Valim and Mikel Lindsaar committed Jan 23, 2010
View
1 actionmailer/lib/action_mailer.rb
@@ -31,6 +31,7 @@ module ActionMailer
extend ::ActiveSupport::Autoload
autoload :AdvAttrAccessor
+ autoload :Collector
autoload :Base
autoload :DeliveryMethods
autoload :DeprecatedApi
View
28 actionmailer/lib/action_mailer/base.rb
@@ -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
@@ -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])
@@ -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
View
2 actionmailer/lib/action_mailer/mail_helper.rb
@@ -18,7 +18,7 @@ def block_format(text)
# Access the mailer instance.
def mailer #:nodoc:
- @controller
+ @_controller
end
# Access the message instance.
View
67 actionmailer/test/base_test.rb
@@ -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
@@ -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
@@ -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

0 comments on commit c6b1626

Please sign in to comment.
Something went wrong with that request. Please try again.