aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/lib
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/lib
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/lib')
-rw-r--r--actionmailer/lib/action_mailer/base.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 1800ff5839..64f82f24a2 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -267,6 +267,33 @@ module ActionMailer #:nodoc:
# set something in the defaults using a proc, and then set the same thing inside of your
# mailer method, it will get over written by the mailer method.
#
+ # = Callbacks
+ #
+ # You can specify callbacks using before_filter and after_filter for configuring your messages.
+ # This may be useful, for example, when you want to add default inline attachments for all
+ # messages sent out by a certain mailer class:
+ #
+ # class Notifier < ActionMailer::Base
+ # before_filter :add_inline_attachment!
+ #
+ # def welcome
+ # mail
+ # end
+ #
+ # private
+ #
+ # def add_inline_attachment!
+ # attachments.inline["footer.jpg"] = File.read('/path/to/filename.jpg')
+ # end
+ # end
+ #
+ # Callbacks in ActionMailer are implemented using AbstractController::Callbacks, so you
+ # can define and configure callbacks in the same manner that you would use callbacks in
+ # classes that inherit from ActionController::Base.
+ #
+ # Note that unless you have a specific reason to do so, you should prefer using before_filter
+ # rather than after_filter in your ActionMailer classes so that headers are parsed properly.
+ #
# = Configuration options
#
# These options are specified on the class level, like
@@ -330,6 +357,7 @@ module ActionMailer #:nodoc:
include AbstractController::Helpers
include AbstractController::Translation
include AbstractController::AssetPaths
+ include AbstractController::Callbacks
self.protected_instance_variables = [:@_action_has_layout]