diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2014-01-26 12:03:32 +0000 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2014-01-26 12:05:35 +0000 |
commit | 4df9cc29c15634de8de5912d2b40766f04e58c03 (patch) | |
tree | 6c12e7208e685b8e270c1210bc257e7fae2ab376 | |
parent | 7d86352f83079458dd04df2ee5e1b38179c3aac3 (diff) | |
download | rails-4df9cc29c15634de8de5912d2b40766f04e58c03.tar.gz rails-4df9cc29c15634de8de5912d2b40766f04e58c03.tar.bz2 rails-4df9cc29c15634de8de5912d2b40766f04e58c03.zip |
Support underscored symbols in Action Mailer config
We allow the use of underscored symbols to represent classes throughout
other parts of Rails so it seems incongruous that it's not supported in
`register_interceptor` and `register_observer`.
-rw-r--r-- | actionmailer/CHANGELOG.md | 5 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 23 | ||||
-rw-r--r-- | actionmailer/test/base_test.rb | 14 |
3 files changed, 37 insertions, 5 deletions
diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md index ede8cfccbf..c264c710f6 100644 --- a/actionmailer/CHANGELOG.md +++ b/actionmailer/CHANGELOG.md @@ -1,3 +1,8 @@ +* Support the use of underscored symbols when registering interceptors and + observers like we do elsewhere within Rails. + + *Andrew White* + * Add the ability to intercept emails before previewing in a similar fashion to how emails can be intercepted before delivery, e.g: diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 5af0217973..76814d336b 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -444,18 +444,31 @@ module ActionMailer end # Register an Observer which will be notified when mail is delivered. - # Either a class or a string can be passed in as the Observer. If a string is passed in - # it will be <tt>constantize</tt>d. + # Either a class, string or symbol can be passed in as the Observer. + # If a string or symbol is passed in it will be camelized and constantized. def register_observer(observer) - delivery_observer = (observer.is_a?(String) ? observer.constantize : observer) + delivery_observer = case observer + when String, Symbol + observer.to_s.camelize.constantize + else + observer + end + Mail.register_observer(delivery_observer) end # Register an Interceptor which will be called before mail is sent. - # Either a class or a string can be passed in as the Interceptor. If a string is passed in + # Either a class, string or symbol can be passed in as the Interceptor. + # If a string or symbol is passed in it will be camelized and constantized. # it will be <tt>constantize</tt>d. def register_interceptor(interceptor) - delivery_interceptor = (interceptor.is_a?(String) ? interceptor.constantize : interceptor) + delivery_interceptor = case interceptor + when String, Symbol + interceptor.to_s.camelize.constantize + else + interceptor + end + Mail.register_interceptor(delivery_interceptor) end diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 454e1afe97..02707d0b5f 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -530,6 +530,13 @@ class BaseTest < ActiveSupport::TestCase mail.deliver end + test "you can register an observer using its symbolized underscored name to the mail object that gets informed on email delivery" do + ActionMailer::Base.register_observer(:"base_test/my_observer") + mail = BaseMailer.welcome + MyObserver.expects(:delivered_email).with(mail) + mail.deliver + end + test "you can register multiple observers to the mail object that both get informed on email delivery" do ActionMailer::Base.register_observers("BaseTest::MyObserver", MySecondObserver) mail = BaseMailer.welcome @@ -568,6 +575,13 @@ class BaseTest < ActiveSupport::TestCase mail.deliver end + test "you can register an interceptor using its symbolized underscored name to the mail object that gets passed the mail object before delivery" do + ActionMailer::Base.register_interceptor(:"base_test/my_interceptor") + mail = BaseMailer.welcome + MyInterceptor.expects(:delivering_email).with(mail) + mail.deliver + end + test "you can register multiple interceptors to the mail object that both get passed the mail object before delivery" do ActionMailer::Base.register_interceptors("BaseTest::MyInterceptor", MySecondInterceptor) mail = BaseMailer.welcome |