2017-07-23 11:17:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:01:31 -04:00
|
|
|
require "active_support/descendants_tracker"
|
2013-12-16 00:52:58 -05:00
|
|
|
|
|
|
|
module ActionMailer
|
|
|
|
module Previews #:nodoc:
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
# Set the location of mailer previews through app configuration:
|
|
|
|
#
|
|
|
|
# config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews"
|
|
|
|
#
|
2014-01-26 06:50:36 -05:00
|
|
|
mattr_accessor :preview_path, instance_writer: false
|
2014-01-26 06:38:30 -05:00
|
|
|
|
2014-06-29 10:12:25 -04:00
|
|
|
# Enable or disable mailer previews through app configuration:
|
|
|
|
#
|
2014-07-01 12:09:24 -04:00
|
|
|
# config.action_mailer.show_previews = true
|
2014-06-29 10:12:25 -04:00
|
|
|
#
|
2017-08-29 14:29:41 -04:00
|
|
|
# Defaults to +true+ for development environment
|
2014-06-29 10:12:25 -04:00
|
|
|
#
|
2014-07-01 12:09:24 -04:00
|
|
|
mattr_accessor :show_previews, instance_writer: false
|
2014-06-29 10:12:25 -04:00
|
|
|
|
2014-01-26 06:38:30 -05:00
|
|
|
# :nodoc:
|
2017-05-31 05:16:20 -04:00
|
|
|
mattr_accessor :preview_interceptors, instance_writer: false, default: [ActionMailer::InlinePreviewInterceptor]
|
2014-08-14 06:50:46 -04:00
|
|
|
end
|
2014-01-26 06:38:30 -05:00
|
|
|
|
2014-08-14 06:50:46 -04:00
|
|
|
module ClassMethods
|
2014-01-26 06:38:30 -05:00
|
|
|
# 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
|
|
|
|
|
2018-05-30 17:36:24 -04:00
|
|
|
# Unregister one or more previously registered Interceptors.
|
|
|
|
def unregister_preview_interceptors(*interceptors)
|
|
|
|
interceptors.flatten.compact.each { |interceptor| unregister_preview_interceptor(interceptor) }
|
|
|
|
end
|
|
|
|
|
2014-02-16 19:05:55 -05:00
|
|
|
# Register an Interceptor which will be called before mail is previewed.
|
2014-01-26 06:38:30 -05:00
|
|
|
# Either a class or a string can be passed in as the Interceptor. If a
|
2017-08-29 14:29:41 -04:00
|
|
|
# string is passed in it will be constantized.
|
2014-01-26 06:38:30 -05:00
|
|
|
def register_preview_interceptor(interceptor)
|
2018-05-30 17:36:24 -04:00
|
|
|
preview_interceptor = interceptor_class_for(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_interceptors.delete(interceptor_class_for(interceptor))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def interceptor_class_for(interceptor)
|
2016-09-01 18:43:33 -04:00
|
|
|
case interceptor
|
|
|
|
when String, Symbol
|
|
|
|
interceptor.to_s.camelize.constantize
|
2015-11-23 17:00:43 -05:00
|
|
|
else
|
2016-09-01 18:43:33 -04:00
|
|
|
interceptor
|
2015-11-23 17:00:43 -05:00
|
|
|
end
|
2014-01-26 06:38:30 -05:00
|
|
|
end
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Preview
|
|
|
|
extend ActiveSupport::DescendantsTracker
|
|
|
|
|
2016-05-29 20:15:18 -04:00
|
|
|
attr_reader :params
|
|
|
|
|
|
|
|
def initialize(params = {})
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
2013-12-16 00:52:58 -05:00
|
|
|
class << self
|
2015-09-28 11:47:44 -04:00
|
|
|
# Returns all mailer preview classes.
|
2013-12-16 00:52:58 -05:00
|
|
|
def all
|
|
|
|
load_previews if descendants.empty?
|
|
|
|
descendants
|
|
|
|
end
|
|
|
|
|
2014-01-26 06:38:30 -05:00
|
|
|
# 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.
|
2016-05-29 20:15:18 -04:00
|
|
|
def call(email, params = {})
|
|
|
|
preview = new(params)
|
2014-01-26 06:38:30 -05:00
|
|
|
message = preview.public_send(email)
|
|
|
|
inform_preview_interceptors(message)
|
|
|
|
message
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
|
2015-09-28 11:47:44 -04:00
|
|
|
# Returns all of the available email previews.
|
2013-12-16 00:52:58 -05:00
|
|
|
def emails
|
|
|
|
public_instance_methods(false).map(&:to_s).sort
|
|
|
|
end
|
|
|
|
|
2017-08-29 14:29:41 -04:00
|
|
|
# Returns +true+ if the email exists.
|
2013-12-16 00:52:58 -05:00
|
|
|
def email_exists?(email)
|
|
|
|
emails.include?(email)
|
|
|
|
end
|
|
|
|
|
2017-08-29 14:29:41 -04:00
|
|
|
# Returns +true+ if the preview exists.
|
2013-12-16 00:52:58 -05:00
|
|
|
def exists?(preview)
|
2016-08-16 03:30:11 -04:00
|
|
|
all.any? { |p| p.preview_name == preview }
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
|
2015-09-28 11:47:44 -04:00
|
|
|
# Find a mailer preview by its underscored class name.
|
2013-12-16 00:52:58 -05:00
|
|
|
def find(preview)
|
2016-08-16 03:30:11 -04:00
|
|
|
all.find { |p| p.preview_name == preview }
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
|
2015-09-28 11:47:44 -04:00
|
|
|
# Returns the underscored name of the mailer preview without the suffix.
|
2013-12-16 00:52:58 -05:00
|
|
|
def preview_name
|
2020-05-24 14:15:38 -04:00
|
|
|
name.delete_suffix("Preview").underscore
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
|
2016-12-22 04:57:02 -05:00
|
|
|
private
|
|
|
|
def load_previews
|
2014-01-26 06:50:36 -05:00
|
|
|
if preview_path
|
2017-11-13 18:16:27 -05:00
|
|
|
Dir["#{preview_path}/**/*_preview.rb"].sort.each { |file| require_dependency file }
|
2014-01-04 13:42:34 -05:00
|
|
|
end
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
|
2016-12-22 04:57:02 -05:00
|
|
|
def preview_path
|
2013-12-16 00:52:58 -05:00
|
|
|
Base.preview_path
|
|
|
|
end
|
2014-01-04 13:42:34 -05:00
|
|
|
|
2016-12-22 04:57:02 -05:00
|
|
|
def show_previews
|
2014-07-01 12:09:24 -04:00
|
|
|
Base.show_previews
|
2014-06-29 10:12:25 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 04:57:02 -05:00
|
|
|
def inform_preview_interceptors(message)
|
2014-01-26 06:38:30 -05:00
|
|
|
Base.preview_interceptors.each do |interceptor|
|
|
|
|
interceptor.previewing_email(message)
|
|
|
|
end
|
|
|
|
end
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|