From b37e8482caee6d406ac5933fceb60cd2b74adfb7 Mon Sep 17 00:00:00 2001
From: "Christian Felder (masone)" <ema@rh-productions.ch>
Date: Fri, 19 Sep 2014 10:32:08 +0200
Subject: Allow attaching files while the mail view is rendered

---
 actionmailer/lib/action_mailer/base.rb                     |  3 ++-
 actionmailer/lib/action_mailer/mail_helper.rb              |  2 +-
 actionmailer/test/base_test.rb                             | 14 ++++++++++++++
 .../base_test/late_inline_attachment_mailer/on_render.erb  |  7 +++++++
 4 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 actionmailer/test/fixtures/base_test/late_inline_attachment_mailer/on_render.erb

(limited to 'actionmailer')

diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index f539fc53c6..1ab68d2953 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -775,7 +775,6 @@ module ActionMailer
     def mail(headers = {}, &block)
       return @_message if @_mail_was_called && headers.blank? && !block
 
-      @_mail_was_called = true
       m = @_message
 
       # At the beginning, do not consider class default for content_type
@@ -803,6 +802,8 @@ module ActionMailer
 
       # Render the templates and blocks
       responses = collect_responses(headers, &block)
+      @_mail_was_called = true
+
       create_parts_from_responses(m, responses)
 
       # Setup content type, reapply charset and handle parts order
diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb
index 483277af04..cc7935a7e0 100644
--- a/actionmailer/lib/action_mailer/mail_helper.rb
+++ b/actionmailer/lib/action_mailer/mail_helper.rb
@@ -29,7 +29,7 @@ module ActionMailer
 
     # Access the message attachments list.
     def attachments
-      @_message.attachments
+      mailer.attachments
     end
 
     # Returns +text+ wrapped at +len+ columns and indented +indent+ spaces.
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb
index d00f5aea47..dcb6959543 100644
--- a/actionmailer/test/base_test.rb
+++ b/actionmailer/test/base_test.rb
@@ -259,6 +259,20 @@ class BaseTest < ActiveSupport::TestCase
     assert_match(/Can't add attachments after `mail` was called./, e.message)
   end
 
+  test "adding inline attachments while rendering mail works" do
+    class LateInlineAttachmentMailer < ActionMailer::Base
+      def on_render
+        mail from: "welcome@example.com", to: "to@example.com"
+      end
+    end
+
+    mail = LateInlineAttachmentMailer.on_render
+    assert_nothing_raised { mail.message }
+
+    assert_equal ["image/jpeg; filename=controller_attachments.jpg",
+                  "image/jpeg; filename=attachments.jpg"], mail.attachments.inline.map {|a| a['Content-Type'].to_s }
+  end
+
   test "accessing attachments works after mail was called" do
     class LateAttachmentAccessorMailer < ActionMailer::Base
       def welcome
diff --git a/actionmailer/test/fixtures/base_test/late_inline_attachment_mailer/on_render.erb b/actionmailer/test/fixtures/base_test/late_inline_attachment_mailer/on_render.erb
new file mode 100644
index 0000000000..6decd3bb31
--- /dev/null
+++ b/actionmailer/test/fixtures/base_test/late_inline_attachment_mailer/on_render.erb
@@ -0,0 +1,7 @@
+<h1>Adding an inline image while rendering</h1>
+
+<% controller.attachments.inline["controller_attachments.jpg"] = 'via controller.attachments.inline' %>
+<%= image_tag attachments['controller_attachments.jpg'].url %>
+
+<% attachments.inline["attachments.jpg"] = 'via attachments.inline' %>
+<%= image_tag attachments['attachments.jpg'].url %>
-- 
cgit v1.2.3