Skip to content
Browse files

Setting the html_ or text_part to nil removes it.

  • Loading branch information...
1 parent cd9e584 commit 39a4805b3126a1b5ccd5ea0b484bec93a3c4b3eb @jeremy jeremy committed Jan 26, 2013
Showing with 59 additions and 20 deletions.
  1. +1 −0 CHANGELOG.rdoc
  2. +26 −10 lib/mail/message.rb
  3. +32 −10 spec/mail/mime_messages_spec.rb
View
1 CHANGELOG.rdoc
@@ -1,6 +1,7 @@
== HEAD
Features:
+* Setting the html_ or text_part to nil removes it (jeremy)
* Close pull request 399 - Accept :ca_path and :ca_file options for SMTP delivery (ndbroadbent)
Performance:
View
36 lib/mail/message.rb
@@ -1581,27 +1581,43 @@ def text_part(&block)
# Helper to add a html part to a multipart/alternative email. If this and
# text_part are both defined in a message, then it will be a multipart/alternative
# message and set itself that way.
- def html_part=(msg = nil)
+ def html_part=(msg)
+ # Assign the html part and set multipart/alternative if there's a text part.
if msg
@html_part = msg
- else
- @html_part = Mail::Part.new('Content-Type: text/html;')
+ add_multipart_alternate_header if text_part
+ add_part @html_part
+
+ # If nil, delete the html part and back out of multipart/alternative.
+ elsif @html_part
+ parts.delete @html_part
+ @html_part = nil
+ if text_part
+ self.content_type = nil
+ body.boundary = nil
+ end
end
- add_multipart_alternate_header unless text_part.blank?
- add_part(@html_part)
end
# Helper to add a text part to a multipart/alternative email. If this and
# html_part are both defined in a message, then it will be a multipart/alternative
# message and set itself that way.
- def text_part=(msg = nil)
+ def text_part=(msg)
+ # Assign the text part and set multipart/alternative if there's an html part.
if msg
@text_part = msg
- else
- @text_part = Mail::Part.new('Content-Type: text/plain;')
+ add_multipart_alternate_header if html_part
+ add_part @text_part
+
+ # If nil, delete the text part and back out of multipart/alternative.
+ elsif @text_part
+ parts.delete @text_part
+ @text_part = nil
+ if html_part
+ self.content_type = nil
+ body.boundary = nil
+ end
end
- add_multipart_alternate_header unless html_part.blank?
- add_part(@text_part)
end
# Adds a part to the parts list or creates the part list
View
42 spec/mail/mime_messages_spec.rb
@@ -137,23 +137,17 @@
mail.boundary.should eq nil
end
- it "should allow you to assign a text part" do
- mail = Mail.new
- text_mail = Mail.new("This is Text")
- doing { mail.text_part = text_mail }.should_not raise_error
- end
-
it "should assign the text part and allow you to reference" do
mail = Mail.new
text_mail = Mail.new("This is Text")
mail.text_part = text_mail
mail.text_part.should eq text_mail
end
- it "should allow you to assign a html part" do
+ it "should not assign a nil text part" do
mail = Mail.new
- html_mail = Mail.new("<b>This is HTML</b>")
- doing { mail.text_part = html_mail }.should_not raise_error
+ mail.text_part = nil
+ mail.text_part.should be_nil
end
it "should assign the html part and allow you to reference" do
@@ -163,6 +157,12 @@
mail.html_part.should eq html_mail
end
+ it "should not assign a nil html part" do
+ mail = Mail.new
+ mail.html_part = nil
+ mail.html_part.should be_nil
+ end
+
it "should add the html part and text part" do
mail = Mail.new
mail.text_part = Mail::Part.new do
@@ -177,7 +177,29 @@
mail.parts.last.class.should eq Mail::Part
end
- it "should set the content type to multipart/alternative if you use the html_part and text_part helpers" do
+ it "should remove the html part and back out of multipart/alternative if set to nil" do
+ mail = Mail.new
+ mail.text_part = Mail::Part.new
+ mail.html_part = Mail::Part.new
+
+ mail.html_part = nil
+ mail.parts.length.should eq 1
+ mail.boundary.should be_nil
+ mail.content_type.should be_nil
+ end
+
+ it "should remove the text part and back out of multipart/alternative if set to nil" do
+ mail = Mail.new
+ mail.text_part = Mail::Part.new
+ mail.html_part = Mail::Part.new
+
+ mail.text_part = nil
+ mail.parts.length.should eq 1
+ mail.boundary.should be_nil
+ mail.content_type.should be_nil
+ end
+
+ it "should set the content type to multipart/alternative if you assign html and text parts" do
mail = Mail.new
mail.text_part = Mail::Part.new do
body "This is Text"

0 comments on commit 39a4805

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