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`.
This commit is contained in:
Andrew White 2014-01-26 12:03:32 +00:00
parent 7d86352f83
commit 4df9cc29c1
3 changed files with 37 additions and 5 deletions

View File

@ -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:

View File

@ -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

View File

@ -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