2010-02-17 06:25:20 -05:00
|
|
|
class Devise::Mailer < ::ActionMailer::Base
|
|
|
|
include Devise::Controllers::ScopedViews
|
2010-09-20 04:47:03 -04:00
|
|
|
attr_reader :scope_name, :resource
|
2009-11-02 20:14:27 -05:00
|
|
|
|
2009-10-07 21:08:24 -04:00
|
|
|
def confirmation_instructions(record)
|
2009-10-19 23:28:01 -04:00
|
|
|
setup_mail(record, :confirmation_instructions)
|
2009-10-07 21:08:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_password_instructions(record)
|
2009-10-19 23:28:01 -04:00
|
|
|
setup_mail(record, :reset_password_instructions)
|
2009-10-07 21:08:24 -04:00
|
|
|
end
|
2009-10-08 10:40:53 -04:00
|
|
|
|
2009-12-30 12:19:33 -05:00
|
|
|
def unlock_instructions(record)
|
|
|
|
setup_mail(record, :unlock_instructions)
|
|
|
|
end
|
2010-09-21 04:07:02 -04:00
|
|
|
|
2009-10-08 10:40:53 -04:00
|
|
|
private
|
2010-09-21 04:07:02 -04:00
|
|
|
|
2010-06-12 14:29:43 -04:00
|
|
|
# Configure default email options
|
|
|
|
def setup_mail(record, action)
|
2010-09-21 04:07:02 -04:00
|
|
|
initialize_from_record(record)
|
2010-09-20 04:47:03 -04:00
|
|
|
mail headers_for(action)
|
|
|
|
end
|
2010-09-21 04:07:02 -04:00
|
|
|
|
2010-09-20 04:47:03 -04:00
|
|
|
def initialize_from_record(record)
|
|
|
|
@scope_name = Devise::Mapping.find_scope!(record)
|
|
|
|
@resource = instance_variable_set("@#{devise_mapping.name}", record)
|
|
|
|
end
|
2010-09-21 04:07:02 -04:00
|
|
|
|
2010-09-20 04:47:03 -04:00
|
|
|
def devise_mapping
|
|
|
|
@devise_mapping ||= Devise.mappings[scope_name]
|
|
|
|
end
|
2010-09-21 04:07:02 -04:00
|
|
|
|
2010-09-20 04:47:03 -04:00
|
|
|
def headers_for(action)
|
2010-06-12 14:29:43 -04:00
|
|
|
headers = {
|
2010-09-20 04:47:03 -04:00
|
|
|
:subject => translate(devise_mapping, action),
|
|
|
|
:from => mailer_sender(devise_mapping),
|
|
|
|
:to => resource.email,
|
2010-07-05 07:27:15 -04:00
|
|
|
:template_path => template_paths
|
2010-06-12 14:29:43 -04:00
|
|
|
}
|
2010-09-21 04:07:02 -04:00
|
|
|
|
2010-09-20 04:47:03 -04:00
|
|
|
if resource.respond_to?(:headers_for)
|
|
|
|
headers.merge!(resource.headers_for(action))
|
|
|
|
end
|
2010-09-21 04:07:02 -04:00
|
|
|
|
|
|
|
unless headers.key?(:reply_to)
|
|
|
|
headers[:reply_to] = headers[:from]
|
|
|
|
end
|
|
|
|
|
2010-09-20 04:47:03 -04:00
|
|
|
headers
|
2010-06-12 14:29:43 -04:00
|
|
|
end
|
2010-04-13 17:11:12 -04:00
|
|
|
|
2010-06-12 14:29:43 -04:00
|
|
|
def mailer_sender(mapping)
|
|
|
|
if Devise.mailer_sender.is_a?(Proc)
|
|
|
|
Devise.mailer_sender.call(mapping.name)
|
|
|
|
else
|
|
|
|
Devise.mailer_sender
|
2010-01-13 11:45:02 -05:00
|
|
|
end
|
2010-06-12 14:29:43 -04:00
|
|
|
end
|
2010-01-13 11:45:02 -05:00
|
|
|
|
2010-07-05 07:27:15 -04:00
|
|
|
def template_paths
|
|
|
|
template_path = [self.class.mailer_name]
|
2010-11-20 15:41:26 -05:00
|
|
|
template_path.unshift "#{@devise_mapping.scoped_path}/mailer" if self.class.scoped_views?
|
2010-07-05 07:27:15 -04:00
|
|
|
template_path
|
|
|
|
end
|
|
|
|
|
2010-06-12 14:29:43 -04:00
|
|
|
# Setup a subject doing an I18n lookup. At first, it attemps to set a subject
|
|
|
|
# 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: '...'
|
|
|
|
#
|
|
|
|
def translate(mapping, key)
|
|
|
|
I18n.t(:"#{mapping.name}_subject", :scope => [:devise, :mailer, key],
|
|
|
|
:default => [:subject, key.to_s.humanize])
|
|
|
|
end
|
2009-10-07 21:08:24 -04:00
|
|
|
end
|