aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer
diff options
context:
space:
mode:
authorJosé Valim and Mikel Lindsaar <pair@programming.com>2010-01-23 21:37:34 +1100
committerJosé Valim and Mikel Lindsaar <pair@programming.com>2010-01-23 21:37:34 +1100
commitc6b16260fe3d1435848e78415bd0b40c10ad7424 (patch)
tree28000447fed5bdaf954b796fcb4c08f66af816aa /actionmailer
parent502028a32bfa46ad18337d2a69f03e8b5dec3c4a (diff)
downloadrails-c6b16260fe3d1435848e78415bd0b40c10ad7424.tar.gz
rails-c6b16260fe3d1435848e78415bd0b40c10ad7424.tar.bz2
rails-c6b16260fe3d1435848e78415bd0b40c10ad7424.zip
Added basic explicit multipart rendering and tests
Diffstat (limited to 'actionmailer')
-rw-r--r--actionmailer/lib/action_mailer.rb1
-rw-r--r--actionmailer/lib/action_mailer/base.rb28
-rw-r--r--actionmailer/lib/action_mailer/mail_helper.rb2
-rw-r--r--actionmailer/test/base_test.rb67
4 files changed, 79 insertions, 19 deletions
diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb
index 37c95baea7..67466e15e2 100644
--- a/actionmailer/lib/action_mailer.rb
+++ b/actionmailer/lib/action_mailer.rb
@@ -31,6 +31,7 @@ module ActionMailer
extend ::ActiveSupport::Autoload
autoload :AdvAttrAccessor
+ autoload :Collector
autoload :Base
autoload :DeliveryMethods
autoload :DeprecatedApi
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 94e20d4b63..28e4c88b5a 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -297,8 +297,9 @@ module ActionMailer #:nodoc:
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 @@ module ActionMailer #:nodoc:
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 @@ module ActionMailer #:nodoc:
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
diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb
index 45ba6f0714..adba94cbef 100644
--- a/actionmailer/lib/action_mailer/mail_helper.rb
+++ b/actionmailer/lib/action_mailer/mail_helper.rb
@@ -18,7 +18,7 @@ module ActionMailer
# Access the mailer instance.
def mailer #:nodoc:
- @controller
+ @_controller
end
# Access the message instance.
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb
index df0eed695d..bae50868ae 100644
--- a/actionmailer/test/base_test.rb
+++ b/actionmailer/test/base_test.rb
@@ -69,6 +69,14 @@ class BaseTest < ActiveSupport::TestCase
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 @@ class BaseTest < ActiveSupport::TestCase
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 @@ class BaseTest < ActiveSupport::TestCase
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