Skip to content
This repository
Browse code

Set sort order for explicit parts from the collector's template sequence

  • Loading branch information...
commit e7e4ed48df3048f32d94e398e99d0048a66ba67e 1 parent c985a0e
authored January 24, 2010
12  actionmailer/lib/action_mailer/base.rb
@@ -415,7 +415,7 @@ def mail(headers = {})
415 415
       # Should be removed when old API is deprecated
416 416
       @mail_was_called = true
417 417
 
418  
-      m, sort_parts = @message, true
  418
+      m = @message
419 419
 
420 420
       # Give preference to headers and fallback to the ones set in mail
421 421
       content_type = headers[:content_type] || m.content_type
@@ -425,12 +425,16 @@ def mail(headers = {})
425 425
       headers[:subject] ||= default_subject
426 426
       quote_fields(m, headers, charset)
427 427
 
  428
+      sort_order = headers[:parts_order] || self.class.default_implicit_parts_order.dup
  429
+
428 430
       responses = if headers[:body]
429 431
         [ { :body => headers[:body], :content_type => self.class.default_content_type.dup } ]
430 432
       elsif block_given?
431  
-        sort_parts = false
432 433
         collector = ActionMailer::Collector.new(self) { render(action_name) }
433 434
         yield(collector)
  435
+        # Collect the sort order of the parts from the collector as Mail will always
  436
+        # sort parts on encode into a "sane" sequence.
  437
+        sort_order = collector.responses.map { |r| r[:content_type] }
434 438
         collector.responses
435 439
       else
436 440
         # TODO Ensure that we don't need to pass I18n.locale as detail
@@ -447,8 +451,8 @@ def mail(headers = {})
447 451
       m.charset      = charset
448 452
       m.mime_version = mime_version
449 453
 
450  
-      if sort_parts && m.parts.present?
451  
-        m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order.dup)
  454
+      if m.multipart?
  455
+        m.body.set_sort_order(sort_order)
452 456
         m.body.sort_parts!
453 457
       end
454 458
 
19  actionmailer/test/base_test.rb
@@ -304,16 +304,15 @@ def explicit_multipart_with_any(hash = {})
304 304
     assert_equal("HTML Explicit Multipart", email.parts[1].parts[1].body.encoded)
305 305
   end
306 306
 
307  
-  # TODO Seems Mail is sorting the templates automatically, and not on demand
308  
-  # test "explicit multipart with templates" do
309  
-  #   email = BaseMailer.deliver_explicit_multipart_templates
310  
-  #   assert_equal(2, email.parts.size)
311  
-  #   assert_equal("multipart/alternate", email.mime_type)
312  
-  #   assert_equal("text/html", email.parts[0].mime_type)
313  
-  #   assert_equal("HTML Explicit Multipart Templates", email.parts[0].body.encoded)
314  
-  #   assert_equal("text/plain", email.parts[1].mime_type)
315  
-  #   assert_equal("TEXT Explicit Multipart Templates", email.parts[1].body.encoded)
316  
-  # end
  307
+  test "explicit multipart with templates" do
  308
+    email = BaseMailer.deliver_explicit_multipart_templates
  309
+    assert_equal(2, email.parts.size)
  310
+    assert_equal("multipart/alternate", email.mime_type)
  311
+    assert_equal("text/html", email.parts[0].mime_type)
  312
+    assert_equal("HTML Explicit Multipart Templates", email.parts[0].body.encoded)
  313
+    assert_equal("text/plain", email.parts[1].mime_type)
  314
+    assert_equal("TEXT Explicit Multipart Templates", email.parts[1].body.encoded)
  315
+  end
317 316
 
318 317
   test "explicit multipart with any" do
319 318
     email = BaseMailer.deliver_explicit_multipart_with_any

0 notes on commit e7e4ed4

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