From ff5fcf65ec1f5963e5168786277eb4437c065524 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Sun, 11 Jan 2015 12:45:08 +0000 Subject: ActionMailer::Base can unregister interceptor(s). One or multiple mail interceptors can be unregistered using `ActionMailer::Base.unregister_interceptors` or `ActionMailer::Base.unregister_interceptor`. For preview interceptors, it's possible to use `ActionMailer::Base.unregister_preview_interceptors` or `ActionMailer::Base.unregister_preview_interceptor`. Refactors logic to constantize a string/symbol into separate method. --- actionmailer/lib/action_mailer/base.rb | 38 +++++++++++++++++++++---------- actionmailer/lib/action_mailer/preview.rb | 32 ++++++++++++++++++++------ 2 files changed, 51 insertions(+), 19 deletions(-) (limited to 'actionmailer/lib') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 53cc1fdb31..d5d124cbb8 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -434,6 +434,7 @@ module ActionMailer helper ActionMailer::MailHelper private_class_method :new #:nodoc: + private_class_method :find_class #:nodoc: class_attribute :default_params self.default_params = { @@ -454,16 +455,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 = case observer - when String, Symbol - observer.to_s.camelize.constantize - else - observer - end + delivery_observer = find_class(observer) Mail.register_observer(delivery_observer) end @@ -472,16 +473,20 @@ 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 = case interceptor - when String, Symbol - interceptor.to_s.camelize.constantize - else - interceptor - end + delivery_interceptor = find_class(interceptor) 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 @@ -835,6 +840,15 @@ 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 44cf6665ba..6fd3b0b120 100644 --- a/actionmailer/lib/action_mailer/preview.rb +++ b/actionmailer/lib/action_mailer/preview.rb @@ -30,21 +30,39 @@ 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 = case interceptor - when String, Symbol - interceptor.to_s.camelize.constantize - else - interceptor - end - + preview_interceptor = find_class(interceptor) 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 -- cgit v1.2.3