diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2014-01-26 11:38:30 +0000 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2014-01-26 12:05:35 +0000 |
commit | 35fd81672e02497c31f0b3cab965f4a38090229a (patch) | |
tree | 950f7215b62424e4b96c1efbfb30ce49a503efde /actionmailer/lib | |
parent | 14d59d2a257aee62c5b02cc18add38fccafad875 (diff) | |
download | rails-35fd81672e02497c31f0b3cab965f4a38090229a.tar.gz rails-35fd81672e02497c31f0b3cab965f4a38090229a.tar.bz2 rails-35fd81672e02497c31f0b3cab965f4a38090229a.zip |
Add the ability to intercept emails before previewing
To support the ability for tools like CSS style inliners to operate on emails
being previewed this commit adds a hook in a similar fashion to the existing
delivery interceptor hook, e.g:
class CSSInlineStyler
def self.previewing_email(message)
# inline CSS styles
end
end
ActionMailer::Base.register_preview_interceptor CSSInlineStyler
Fixes #13622.
Diffstat (limited to 'actionmailer/lib')
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 15 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/preview.rb | 39 |
2 files changed, 52 insertions, 2 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 41db62cc58..5af0217973 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -330,6 +330,21 @@ module ActionMailer # An overview of all previews is accessible at <tt>http://localhost:3000/rails/mailers</tt> # on a running development server instance. # + # Previews can also be intercepted in a similar manner as deliveries can be by registering + # a preview interceptor that has a <tt>previewing_email</tt> method: + # + # class CssInlineStyler + # def self.previewing_email(message) + # # inline CSS styles + # end + # end + # + # config.action_mailer.register_preview_interceptor :css_inline_styler + # + # Note that interceptors need to be registered both with <tt>register_interceptor</tt> + # and <tt>register_preview_interceptor</tt> if they should operate on both sending and + # previewing emails. + # # = Configuration options # # These options are specified on the class level, like diff --git a/actionmailer/lib/action_mailer/preview.rb b/actionmailer/lib/action_mailer/preview.rb index ecceaf8c70..0efd702e4d 100644 --- a/actionmailer/lib/action_mailer/preview.rb +++ b/actionmailer/lib/action_mailer/preview.rb @@ -10,6 +10,31 @@ module ActionMailer # config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews" # class_attribute :preview_path, instance_writer: false + + # :nodoc: + mattr_accessor :preview_interceptors, instance_writer: false + self.preview_interceptors = [] + + # Register one or more Interceptors which will be called before mail is previewed. + def register_preview_interceptors(*interceptors) + interceptors.flatten.compact.each { |interceptor| register_preview_interceptor(interceptor) } + end + + # Register am 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 <tt>constantize</tt>d. + def register_preview_interceptor(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 end end @@ -23,10 +48,14 @@ module ActionMailer descendants end - # Returns the mail object for the given email name + # Returns the mail object for the given email name. The registered preview + # interceptors will be informed so that they can transform the message + # as they would if the mail was actually being delivered. def call(email) preview = self.new - preview.public_send(email) + message = preview.public_send(email) + inform_preview_interceptors(message) + message end # Returns all of the available email previews @@ -68,6 +97,12 @@ module ActionMailer def preview_path? #:nodoc: Base.preview_path? end + + def inform_preview_interceptors(message) #:nodoc: + Base.preview_interceptors.each do |interceptor| + interceptor.previewing_email(message) + end + end end end end |