Skip to content

Commit 4df9cc2

Browse files
committed
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`.
1 parent 7d86352 commit 4df9cc2

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

actionmailer/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Support the use of underscored symbols when registering interceptors and
2+
observers like we do elsewhere within Rails.
3+
4+
*Andrew White*
5+
16
* Add the ability to intercept emails before previewing in a similar fashion
27
to how emails can be intercepted before delivery, e.g:
38

actionmailer/lib/action_mailer/base.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,18 +444,31 @@ def register_interceptors(*interceptors)
444444
end
445445

446446
# Register an Observer which will be notified when mail is delivered.
447-
# Either a class or a string can be passed in as the Observer. If a string is passed in
448-
# it will be <tt>constantize</tt>d.
447+
# Either a class, string or symbol can be passed in as the Observer.
448+
# If a string or symbol is passed in it will be camelized and constantized.
449449
def register_observer(observer)
450-
delivery_observer = (observer.is_a?(String) ? observer.constantize : observer)
450+
delivery_observer = case observer
451+
when String, Symbol
452+
observer.to_s.camelize.constantize
453+
else
454+
observer
455+
end
456+
451457
Mail.register_observer(delivery_observer)
452458
end
453459

454460
# Register an Interceptor which will be called before mail is sent.
455-
# Either a class or a string can be passed in as the Interceptor. If a string is passed in
461+
# Either a class, string or symbol can be passed in as the Interceptor.
462+
# If a string or symbol is passed in it will be camelized and constantized.
456463
# it will be <tt>constantize</tt>d.
457464
def register_interceptor(interceptor)
458-
delivery_interceptor = (interceptor.is_a?(String) ? interceptor.constantize : interceptor)
465+
delivery_interceptor = case interceptor
466+
when String, Symbol
467+
interceptor.to_s.camelize.constantize
468+
else
469+
interceptor
470+
end
471+
459472
Mail.register_interceptor(delivery_interceptor)
460473
end
461474

actionmailer/test/base_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,13 @@ def self.delivered_email(mail)
530530
mail.deliver
531531
end
532532

533+
test "you can register an observer using its symbolized underscored name to the mail object that gets informed on email delivery" do
534+
ActionMailer::Base.register_observer(:"base_test/my_observer")
535+
mail = BaseMailer.welcome
536+
MyObserver.expects(:delivered_email).with(mail)
537+
mail.deliver
538+
end
539+
533540
test "you can register multiple observers to the mail object that both get informed on email delivery" do
534541
ActionMailer::Base.register_observers("BaseTest::MyObserver", MySecondObserver)
535542
mail = BaseMailer.welcome
@@ -568,6 +575,13 @@ def welcome
568575
mail.deliver
569576
end
570577

578+
test "you can register an interceptor using its symbolized underscored name to the mail object that gets passed the mail object before delivery" do
579+
ActionMailer::Base.register_interceptor(:"base_test/my_interceptor")
580+
mail = BaseMailer.welcome
581+
MyInterceptor.expects(:delivering_email).with(mail)
582+
mail.deliver
583+
end
584+
571585
test "you can register multiple interceptors to the mail object that both get passed the mail object before delivery" do
572586
ActionMailer::Base.register_interceptors("BaseTest::MyInterceptor", MySecondInterceptor)
573587
mail = BaseMailer.welcome

0 commit comments

Comments
 (0)