aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/test
diff options
context:
space:
mode:
authorJustin S. Leitgeb <justin@stackbuilders.com>2012-03-10 15:37:04 -0500
committerJustin S. Leitgeb <justin@stackbuilders.com>2012-03-11 14:05:31 -0500
commit4f28c4fc9a51bbab76d5dcde033c47aa6711339b (patch)
tree2e9f67295097de7871d6e2619fa03b2bfdc53e89 /actionmailer/test
parent4d2d0d9906f16a1255e10d55530907318b281c3e (diff)
downloadrails-4f28c4fc9a51bbab76d5dcde033c47aa6711339b.tar.gz
rails-4f28c4fc9a51bbab76d5dcde033c47aa6711339b.tar.bz2
rails-4f28c4fc9a51bbab76d5dcde033c47aa6711339b.zip
Add ability to define callbacks in ActionMailer using AbstractController::Callbacks.
Prior to this commit, there isn't a good way of adding things like default inline attachments to an email. This Stack Overflow thread shows people using hooks like the 'default' method in ActionMailer::Base to call a Proc for message configuration: http://stackoverflow.com/questions/5113121/rails-use-same-attachment-for-all-emails-using-layout This has the unintended side effect of setting a message header, so it's not a good solution. This pull request adds support for message modifications by including AbstractController:Callbacks in ActionMailer::Base. It includes tests and documentation for the functionality provided by including this module.
Diffstat (limited to 'actionmailer/test')
-rw-r--r--actionmailer/test/base_test.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb
index 8d60e028cb..aed3ee1874 100644
--- a/actionmailer/test/base_test.rb
+++ b/actionmailer/test/base_test.rb
@@ -552,6 +552,52 @@ class BaseTest < ActiveSupport::TestCase
assert_equal("Thanks for signing up this afternoon", mail.subject)
end
+ test "modifying the mail message with a before_filter" do
+ class BeforeFilterMailer < ActionMailer::Base
+ before_filter :add_special_header!
+
+ def welcome ; mail ; end
+
+ private
+ def add_special_header!
+ headers('X-Special-Header' => 'Wow, so special')
+ end
+ end
+
+ assert_equal('Wow, so special', BeforeFilterMailer.welcome['X-Special-Header'].to_s)
+ end
+
+ test "modifying the mail message with an after_filter" do
+ class AfterFilterMailer < ActionMailer::Base
+ after_filter :add_special_header!
+
+ def welcome ; mail ; end
+
+ private
+ def add_special_header!
+ headers('X-Special-Header' => 'Testing')
+ end
+ end
+
+ assert_equal('Testing', AfterFilterMailer.welcome['X-Special-Header'].to_s)
+ end
+
+ test "adding an inline attachment using a before_filter" do
+ class DefaultInlineAttachmentMailer < ActionMailer::Base
+ before_filter :add_inline_attachment!
+
+ def welcome ; mail ; end
+
+ private
+ def add_inline_attachment!
+ attachments.inline["footer.jpg"] = 'hey there'
+ end
+ end
+
+ mail = DefaultInlineAttachmentMailer.welcome
+ assert_equal('image/jpeg; filename=footer.jpg', mail.attachments.inline.first['Content-Type'].to_s)
+ end
+
test "action methods should be refreshed after defining new method" do
class FooMailer < ActionMailer::Base
# this triggers action_methods