2017-12-21 17:36:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-06-27 15:13:00 +00:00
|
|
|
module Devise
|
|
|
|
module Mailers
|
|
|
|
module Helpers
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
include Devise::Controllers::ScopedViews
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2016-07-15 19:02:43 +00:00
|
|
|
attr_reader :scope_name, :resource
|
|
|
|
|
2011-06-27 15:13:00 +00:00
|
|
|
# Configure default email options
|
2016-07-13 09:25:35 +00:00
|
|
|
def devise_mail(record, action, opts = {}, &block)
|
2011-06-27 15:13:00 +00:00
|
|
|
initialize_from_record(record)
|
2016-07-13 09:25:35 +00:00
|
|
|
mail headers_for(action, opts), &block
|
2011-06-27 15:13:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_from_record(record)
|
|
|
|
@scope_name = Devise::Mapping.find_scope!(record)
|
|
|
|
@resource = instance_variable_set("@#{devise_mapping.name}", record)
|
|
|
|
end
|
|
|
|
|
|
|
|
def devise_mapping
|
|
|
|
@devise_mapping ||= Devise.mappings[scope_name]
|
|
|
|
end
|
|
|
|
|
2013-01-04 17:34:52 +00:00
|
|
|
def headers_for(action, opts)
|
2011-06-27 15:13:00 +00:00
|
|
|
headers = {
|
2014-02-25 16:42:55 +00:00
|
|
|
subject: subject_for(action),
|
|
|
|
to: resource.email,
|
|
|
|
from: mailer_sender(devise_mapping),
|
|
|
|
reply_to: mailer_reply_to(devise_mapping),
|
|
|
|
template_path: template_paths,
|
|
|
|
template_name: action
|
2013-01-04 17:34:52 +00:00
|
|
|
}.merge(opts)
|
2011-06-27 15:13:00 +00:00
|
|
|
|
2012-12-13 08:59:36 +00:00
|
|
|
@email = headers[:to]
|
|
|
|
headers
|
2011-06-27 15:13:00 +00:00
|
|
|
end
|
|
|
|
|
2012-06-26 22:11:35 +00:00
|
|
|
def mailer_reply_to(mapping)
|
|
|
|
mailer_sender(mapping, :reply_to)
|
|
|
|
end
|
2012-12-13 08:59:36 +00:00
|
|
|
|
2012-06-26 22:11:35 +00:00
|
|
|
def mailer_from(mapping)
|
|
|
|
mailer_sender(mapping, :from)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mailer_sender(mapping, sender = :from)
|
2013-04-12 13:16:33 +00:00
|
|
|
default_sender = default_params[sender]
|
|
|
|
if default_sender.present?
|
2013-04-13 14:35:50 +00:00
|
|
|
default_sender.respond_to?(:to_proc) ? instance_eval(&default_sender) : default_sender
|
2011-08-20 20:35:47 +00:00
|
|
|
elsif Devise.mailer_sender.is_a?(Proc)
|
2011-06-27 15:13:00 +00:00
|
|
|
Devise.mailer_sender.call(mapping.name)
|
|
|
|
else
|
|
|
|
Devise.mailer_sender
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def template_paths
|
2013-01-29 02:08:26 +00:00
|
|
|
template_path = _prefixes.dup
|
2011-06-27 15:13:00 +00:00
|
|
|
template_path.unshift "#{@devise_mapping.scoped_path}/mailer" if self.class.scoped_views?
|
|
|
|
template_path
|
|
|
|
end
|
|
|
|
|
2016-02-02 18:00:17 +00:00
|
|
|
# Set up a subject doing an I18n lookup. At first, it attempts to set a subject
|
2011-06-27 15:13:00 +00:00
|
|
|
# based on the current mapping:
|
|
|
|
#
|
|
|
|
# en:
|
|
|
|
# devise:
|
|
|
|
# mailer:
|
|
|
|
# confirmation_instructions:
|
|
|
|
# user_subject: '...'
|
|
|
|
#
|
|
|
|
# If one does not exist, it fallbacks to ActionMailer default:
|
|
|
|
#
|
|
|
|
# en:
|
|
|
|
# devise:
|
|
|
|
# mailer:
|
|
|
|
# confirmation_instructions:
|
|
|
|
# subject: '...'
|
|
|
|
#
|
2013-01-10 04:26:06 +00:00
|
|
|
def subject_for(key)
|
2014-02-25 16:42:55 +00:00
|
|
|
I18n.t(:"#{devise_mapping.name}_subject", scope: [:devise, :mailer, key],
|
|
|
|
default: [:subject, key.to_s.humanize])
|
2011-06-27 15:13:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-08-20 20:35:47 +00:00
|
|
|
end
|