From a58f1cb99b9f08aec3a8e8b368fa7139220ca85d Mon Sep 17 00:00:00 2001
From: Nobuhiro IMAI <nov@yo.rim.or.jp>
Date: Mon, 5 Oct 2009 12:33:05 +0900
Subject: [PATCH] proper content type for plain text mails

* use "body" instead of "part" if Setting.plain_text_mail?
* update test for plain text mail
---
 app/models/mailer.rb     |   11 ++++++++---
 test/unit/mailer_test.rb |    3 ++-
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/app/models/mailer.rb b/app/models/mailer.rb
index 1a2e35e..3d5231d 100644
--- a/app/models/mailer.rb
+++ b/app/models/mailer.rb
@@ -352,9 +352,14 @@ class Mailer < ActionMailer::Base
   # https://rails.lighthouseapp.com/projects/8994/tickets/1799-actionmailer-doesnt-set-template_format-when-rendering-layouts
   
   def render_multipart(method_name, body)
-    content_type "multipart/alternative"
-    part :content_type => "text/plain", :body => render(:file => "#{method_name}.text.plain.rhtml", :body => body, :layout => 'mailer.text.plain.erb')
-    part :content_type => "text/html", :body => render_message("#{method_name}.text.html.rhtml", body) unless Setting.plain_text_mail?
+    if Setting.plain_text_mail?
+      content_type "text/plain"
+      body render(:file => "#{method_name}.text.plain.rhtml", :body => body, :layout => 'mailer.text.plain.erb')
+    else
+      content_type "multipart/alternative"
+      part :content_type => "text/plain", :body => render(:file => "#{method_name}.text.plain.rhtml", :body => body, :layout => 'mailer.text.plain.erb')
+      part :content_type => "text/html", :body => render_message("#{method_name}.text.html.rhtml", body)
+    end
   end
 
   # Makes partial rendering work with Rails 1.2 (retro-compatibility)
diff --git a/test/unit/mailer_test.rb b/test/unit/mailer_test.rb
index 5024a84..2b8a17c 100644
--- a/test/unit/mailer_test.rb
+++ b/test/unit/mailer_test.rb
@@ -103,7 +103,8 @@ class MailerTest < ActiveSupport::TestCase
     journal = Journal.find(2)
     Mailer.deliver_issue_edit(journal)
     mail = ActionMailer::Base.deliveries.last
-    assert_equal 1, mail.parts.size
+    assert_equal "text/plain", mail.content_type
+    assert_equal 0, mail.parts.size
     assert !mail.encoded.include?('href')
   end
 
-- 
1.6.4.3

