Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ActionMailer should respect content type when choosing layouts

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 1d4554d766dbf8391689d91b4b88766757051c68 1 parent 8458365
Colin Curtin authored lifo committed
15 actionmailer/lib/action_mailer/base.rb
View
@@ -549,7 +549,12 @@ def initialize_defaults(method_name)
end
def render_message(method_name, body)
+ if method_name.respond_to?(:content_type)
+ @current_template_content_type = method_name.content_type
+ end
render :file => method_name, :body => body
+ ensure
+ @current_template_content_type = nil
end
def render(opts)
@@ -568,7 +573,11 @@ def render(opts)
end
def default_template_format
- :html
+ if @current_template_content_type
+ Mime::Type.lookup(@current_template_content_type).to_sym
+ else
+ :html
+ end
end
def candidate_for_layout?(options)
@@ -588,7 +597,9 @@ def template_path
end
def initialize_template_class(assigns)
- ActionView::Base.new(view_paths, assigns, self)
+ template = ActionView::Base.new(view_paths, assigns, self)
+ template.template_format = default_template_format
+ template
end
def sort_parts(parts, order = [])
1  actionmailer/test/fixtures/auto_layout_mailer/multipart.text.html.erb
View
@@ -0,0 +1 @@
+text/html multipart
1  actionmailer/test/fixtures/auto_layout_mailer/multipart.text.plain.erb
View
@@ -0,0 +1 @@
+text/plain multipart
1  actionmailer/test/fixtures/layouts/auto_layout_mailer.text.erb
View
@@ -0,0 +1 @@
+text/plain layout - <%= yield %>
17 actionmailer/test/mail_layout_test.rb
View
@@ -20,6 +20,12 @@ def nolayout(recipient)
from "tester@example.com"
body render(:inline => "Hello, <%= @world %>", :layout => false, :body => { :world => "Earth" })
end
+
+ def multipart(recipient)
+ recipients recipient
+ subject "You have a mail"
+ from "tester@example.com"
+ end
end
class ExplicitLayoutMailer < ActionMailer::Base
@@ -56,6 +62,17 @@ def test_should_pickup_default_layout
assert_equal "Hello from layout Inside", mail.body.strip
end
+ def test_should_pickup_multipart_layout
+ mail = AutoLayoutMailer.create_multipart(@recipient)
+ assert_equal 2, mail.parts.size
+
+ assert_equal 'text/plain', mail.parts.first.content_type
+ assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body
+
+ assert_equal 'text/html', mail.parts.last.content_type
+ assert_equal "Hello from layout text/html multipart", mail.parts.last.body
+ end
+
def test_should_pickup_layout_given_to_render
mail = AutoLayoutMailer.create_spam(@recipient)
assert_equal "Spammer layout Hello, Earth", mail.body.strip
Please sign in to comment.
Something went wrong with that request. Please try again.