From 3faf7485623da55215d6d7f3dcb2eed92c59c699 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 23 Nov 2015 15:00:43 -0700 Subject: [PATCH] Revert "Merge pull request #18446 from cloud8421/actionmailer-unregister-interceptor" This reverts commit 65a61ab7c370d2894c11ce276725f723a5c9c111, reversing changes made to 14314ca18302f18c3d8bb7a63e9f71ac4c2290c2. This PR broke the build --- actionmailer/CHANGELOG.md | 7 -- actionmailer/lib/action_mailer/base.rb | 39 ++++-------- actionmailer/lib/action_mailer/preview.rb | 32 ++-------- actionmailer/test/base_test.rb | 78 ----------------------- 4 files changed, 19 insertions(+), 137 deletions(-) diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md index 2280688cbc5..0ecb0235bce 100644 --- a/actionmailer/CHANGELOG.md +++ b/actionmailer/CHANGELOG.md @@ -1,10 +1,3 @@ -* Add `Base.unregister_interceptor`, `Base.unregister_interceptors`, - `Base.unregister_preview_interceptor` and `Base.unregister_preview_interceptors`. - This makes it possible to dynamically add and remove email interceptors - at runtime in the same way they're registered. - - *Claudio Ortolina* - * `config.force_ssl = true` will set `config.action_mailer.default_url_options = { protocol: 'https' }` diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 4c5d959e32f..cbd7cec70ff 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -441,8 +441,6 @@ module ActionMailer helper ActionMailer::MailHelper - private_class_method :find_class #:nodoc: - class_attribute :default_params self.default_params = { mime_version: "1.0", @@ -462,16 +460,16 @@ module ActionMailer interceptors.flatten.compact.each { |interceptor| register_interceptor(interceptor) } end - # Unregister one or more Interceptors which would be called before mail is sent. - def unregister_interceptors(*interceptors) - interceptors.flatten.compact.each { |interceptor| unregister_interceptor(interceptor) } - end - # Register an Observer which will be notified when mail is delivered. # 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 = find_class(observer) + delivery_observer = case observer + when String, Symbol + observer.to_s.camelize.constantize + else + observer + end Mail.register_observer(delivery_observer) end @@ -480,20 +478,16 @@ module ActionMailer # 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. def register_interceptor(interceptor) - delivery_interceptor = find_class(interceptor) + delivery_interceptor = case interceptor + when String, Symbol + interceptor.to_s.camelize.constantize + else + interceptor + end Mail.register_interceptor(delivery_interceptor) end - # Unregister a previously registered interceptor. - # 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. - def unregister_interceptor(interceptor) - delivery_interceptor = find_class(interceptor) - - Mail.unregister_interceptor(delivery_interceptor) - end - # Returns the name of current mailer. This method is also being used as a path for a view lookup. # If this is an anonymous mailer, this method will return +anonymous+ instead. def mailer_name @@ -847,15 +841,6 @@ module ActionMailer m end - def self.find_class(klass_or_string_or_symbol) - case klass_or_string_or_symbol - when String, Symbol - klass_or_string_or_symbol.to_s.camelize.constantize - else - klass_or_string_or_symbol - end - end - protected # Used by #mail to set the content type of the message. diff --git a/actionmailer/lib/action_mailer/preview.rb b/actionmailer/lib/action_mailer/preview.rb index 0b37f405eed..aab92fe8dbf 100644 --- a/actionmailer/lib/action_mailer/preview.rb +++ b/actionmailer/lib/action_mailer/preview.rb @@ -30,39 +30,21 @@ module ActionMailer interceptors.flatten.compact.each { |interceptor| register_preview_interceptor(interceptor) } end - # Unregister one or more previously registered Interceptors. - def unregister_preview_interceptors(*interceptors) - interceptors.flatten.compact.each { |interceptor| unregister_preview_interceptor(interceptor) } - end - # Register an Interceptor which will be called before mail is previewed. # Either a class or a string can be passed in as the Interceptor. If a # string is passed in it will be constantized. def register_preview_interceptor(interceptor) - preview_interceptor = find_class(interceptor) + preview_interceptor = case interceptor + when String, Symbol + interceptor.to_s.camelize.constantize + else + interceptor + end + unless preview_interceptors.include?(preview_interceptor) preview_interceptors << preview_interceptor end end - - # Unregister a previously registered Interceptor. - # Either a class or a string can be passed in as the Interceptor. If a - # string is passed in it will be constantized. - def unregister_preview_interceptor(interceptor) - preview_interceptor = find_class(interceptor) - preview_interceptors.delete(preview_interceptor) - end - - private - - def find_class(klass_or_string_or_symbol) #:nodoc: - case klass_or_string_or_symbol - when String, Symbol - klass_or_string_or_symbol.to_s.camelize.constantize - else - klass_or_string_or_symbol - end - end end end diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 9828d800446..50f2c717379 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -707,47 +707,6 @@ class BaseTest < ActiveSupport::TestCase end end - test "you can unregister an interceptor to the mail object that gets passed the mail object before delivery" do - mail_side_effects do - ActionMailer::Base.register_interceptor(MyInterceptor) - ActionMailer::Base.unregister_interceptor(MyInterceptor) - mail = BaseMailer.welcome - MyInterceptor.expects(:delivering_email).with(mail).never - mail.deliver_now - end - end - - test "you can unregister an interceptor using its stringified name to the mail object that gets passed the mail object before delivery" do - mail_side_effects do - ActionMailer::Base.register_interceptor("BaseTest::MyInterceptor") - ActionMailer::Base.unregister_interceptor("BaseTest::MyInterceptor") - mail = BaseMailer.welcome - MyInterceptor.expects(:delivering_email).with(mail).never - mail.deliver_now - end - end - - test "you can unregister an interceptor using its symbolized underscored name to the mail object that gets passed the mail object before delivery" do - mail_side_effects do - ActionMailer::Base.register_interceptor(:"base_test/my_interceptor") - ActionMailer::Base.unregister_interceptor(:"base_test/my_interceptor") - mail = BaseMailer.welcome - MyInterceptor.expects(:delivering_email).with(mail).never - mail.deliver_now - end - end - - test "you can unregister multiple interceptors to the mail object that both get passed the mail object before delivery" do - mail_side_effects do - ActionMailer::Base.register_interceptors("BaseTest::MyInterceptor", MySecondInterceptor) - ActionMailer::Base.unregister_interceptors("BaseTest::MyInterceptor", MySecondInterceptor) - mail = BaseMailer.welcome - MyInterceptor.expects(:delivering_email).with(mail).never - MySecondInterceptor.expects(:delivering_email).with(mail).never - mail.deliver_now - end - end - test "being able to put proc's into the defaults hash and they get evaluated on mail sending" do mail1 = ProcMailer.welcome['X-Proc-Method'] yesterday = 1.day.ago @@ -985,41 +944,4 @@ class BasePreviewInterceptorsTest < ActiveSupport::TestCase end end end - - test "you can unregister a preview interceptor to the mail object that gets passed the mail object before previewing" do - ActionMailer::Base.register_preview_interceptor(MyInterceptor) - ActionMailer::Base.unregister_preview_interceptor(MyInterceptor) - mail = BaseMailer.welcome - BaseMailerPreview.any_instance.stubs(:welcome).returns(mail) - MyInterceptor.expects(:previewing_email).with(mail).never - BaseMailerPreview.call(:welcome) - end - - test "you can unregister a preview interceptor using its stringified name to the mail object that gets passed the mail object before previewing" do - ActionMailer::Base.register_preview_interceptor("BasePreviewInterceptorsTest::MyInterceptor") - ActionMailer::Base.unregister_preview_interceptor("BasePreviewInterceptorsTest::MyInterceptor") - mail = BaseMailer.welcome - BaseMailerPreview.any_instance.stubs(:welcome).returns(mail) - MyInterceptor.expects(:previewing_email).with(mail).never - BaseMailerPreview.call(:welcome) - end - - test "you can unregister an interceptor using its symbolized underscored name to the mail object that gets passed the mail object before previewing" do - ActionMailer::Base.register_preview_interceptor(:"base_preview_interceptors_test/my_interceptor") - ActionMailer::Base.unregister_preview_interceptor(:"base_preview_interceptors_test/my_interceptor") - mail = BaseMailer.welcome - BaseMailerPreview.any_instance.stubs(:welcome).returns(mail) - MyInterceptor.expects(:previewing_email).with(mail).never - BaseMailerPreview.call(:welcome) - end - - test "you can unregister multiple preview interceptors to the mail object that both get passed the mail object before previewing" do - ActionMailer::Base.register_preview_interceptors("BasePreviewInterceptorsTest::MyInterceptor", MySecondInterceptor) - ActionMailer::Base.unregister_preview_interceptors("BasePreviewInterceptorsTest::MyInterceptor", MySecondInterceptor) - mail = BaseMailer.welcome - BaseMailerPreview.any_instance.stubs(:welcome).returns(mail) - MyInterceptor.expects(:previewing_email).with(mail).never - MySecondInterceptor.expects(:previewing_email).with(mail).never - BaseMailerPreview.call(:welcome) - end end