Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding options to register observers and interceptors through ActionM…

…ailer::Base.register_observer and ActionMailer::Base.register_interceptor. These hook into Mail.register_interceptor and Mail.register_observer. Also bumped Mail requirement to 2.1.3

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit cefc136ec332e5e065a2f4dd184d6fec0ea3c2ba 1 parent a6684ee
@mikel mikel authored josevalim committed
View
2  actionmailer/actionmailer.gemspec
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
s.has_rdoc = true
s.add_dependency('actionpack', '= 3.0.0.beta1')
- s.add_dependency('mail', '~> 2.1.2')
+ s.add_dependency('mail', '~> 2.1.3')
s.add_dependency('text-format', '~> 1.0.0')
end
View
15 actionmailer/lib/action_mailer/base.rb
@@ -181,6 +181,18 @@ module ActionMailer #:nodoc:
# and the second being a <tt>application/pdf</tt> with a Base64 encoded copy of the file.pdf book
# with the filename +free_book.pdf+.
#
+ # = Observing and Intercepting Mails
+ #
+ # ActionMailer provides hooks into the Mail observer and interceptor methods. These allow you to
+ # register objects that are called during the mail delivery life cycle.
+ #
+ # An observer object must implement the <tt>:delivered_email(message)</tt> method which will be
+ # called once for every email sent after the email has been sent.
+ #
+ # An interceptor object must implement the <tt>:delivering_email(message)</tt> method which will be
+ # called before the email is sent, allowing you to make modifications to the email before it hits
+ # the delivery agents. Your object should make and needed modifications directly to the passed
+ # in Mail::Message instance.
#
# = Configuration options
#
@@ -265,6 +277,9 @@ class Base < AbstractController::Base
include ActionMailer::OldApi
include ActionMailer::DeprecatedApi
+
+ delegate :register_observer, :to => Mail
+ delegate :register_interceptor, :to => Mail
private_class_method :new #:nodoc:
View
26 actionmailer/test/base_test.rb
@@ -502,6 +502,32 @@ def different_layout(layout_name='')
mail = BaseMailer.welcome_from_another_path(['unknown/invalid', 'another.path/base_mailer'])
assert_equal("Welcome from another path", mail.body.encoded)
end
+
+ # Before and After hooks
+
+ class MyObserver
+ def self.delivered_email(mail)
+ end
+ end
+
+ test "you can register an observer to the mail object that gets informed on email delivery" do
+ ActionMailer::Base.register_observer(MyObserver)
+ mail = BaseMailer.welcome
+ MyObserver.expects(:delivered_email).with(mail)
+ mail.deliver
+ end
+
+ class MyInterceptor
+ def self.delivering_email(mail)
+ end
+ end
+
+ test "you can register an interceptor to the mail object that gets passed the mail object before delivery" do
+ ActionMailer::Base.register_interceptor(MyInterceptor)
+ mail = BaseMailer.welcome
+ MyInterceptor.expects(:delivering_email).with(mail)
+ mail.deliver
+ end
protected

0 comments on commit cefc136

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