Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract mailer functionality, closes #1164

  • Loading branch information...
commit 42f028527883bc640e8e2aa5d6eddfd6679bc0ec 1 parent 13ef23f
@josevalim josevalim authored
View
81 app/mailers/devise/mailer.rb
@@ -1,88 +1,15 @@
class Devise::Mailer < ::ActionMailer::Base
- include Devise::Controllers::ScopedViews
- attr_reader :scope_name, :resource
+ include Devise::Mailers::Helpers
def confirmation_instructions(record)
- setup_mail(record, :confirmation_instructions)
+ devise_mail(record, :confirmation_instructions)
end
def reset_password_instructions(record)
- setup_mail(record, :reset_password_instructions)
+ devise_mail(record, :reset_password_instructions)
end
def unlock_instructions(record)
- setup_mail(record, :unlock_instructions)
- end
-
- private
-
- # Configure default email options
- def setup_mail(record, action)
- initialize_from_record(record)
- mail headers_for(action)
- 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
-
- def headers_for(action)
- headers = {
- :subject => translate(devise_mapping, action),
- :from => mailer_sender(devise_mapping),
- :to => resource.email,
- :template_path => template_paths
- }
-
- if resource.respond_to?(:headers_for)
- headers.merge!(resource.headers_for(action))
- end
-
- unless headers.key?(:reply_to)
- headers[:reply_to] = headers[:from]
- end
-
- headers
- end
-
- def mailer_sender(mapping)
- if Devise.mailer_sender.is_a?(Proc)
- Devise.mailer_sender.call(mapping.name)
- else
- Devise.mailer_sender
- end
- end
-
- def template_paths
- template_path = [self.class.mailer_name]
- template_path.unshift "#{@devise_mapping.scoped_path}/mailer" if self.class.scoped_views?
- template_path
- end
-
- # 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])
+ devise_mail(record, :unlock_instructions)
end
end
View
4 lib/devise.rb
@@ -31,6 +31,10 @@ module Encryptors
autoload :Sha1, 'devise/encryptors/sha1'
end
+ module Mailers
+ autoload :Helpers, 'devise/mailers/helpers'
+ end
+
module Strategies
autoload :Base, 'devise/strategies/base'
autoload :Authenticatable, 'devise/strategies/authenticatable'
View
84 lib/devise/mailers/helpers.rb
@@ -0,0 +1,84 @@
+module Devise
+ module Mailers
+ module Helpers
+ extend ActiveSupport::Concern
+
+ included do
+ include Devise::Controllers::ScopedViews
+ attr_reader :scope_name, :resource
+ end
+
+ protected
+
+ # Configure default email options
+ def devise_mail(record, action)
@rymai
rymai added a note

Just so you know, setup_mail was used by devise_invitable so it broke it, I'll pull-request devise_invitable to fix it right away.

@josevalim Owner

Ugh, this is bad. I will add a deprecation warning to Devise as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ initialize_from_record(record)
+ mail headers_for(action)
+ 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
+
+ def headers_for(action)
+ headers = {
+ :subject => translate(devise_mapping, action),
+ :from => mailer_sender(devise_mapping),
+ :to => resource.email,
+ :template_path => template_paths
+ }
+
+ if resource.respond_to?(:headers_for)
+ headers.merge!(resource.headers_for(action))
+ end
+
+ unless headers.key?(:reply_to)
+ headers[:reply_to] = headers[:from]
+ end
+
+ headers
+ end
+
+ def mailer_sender(mapping)
+ if Devise.mailer_sender.is_a?(Proc)
+ Devise.mailer_sender.call(mapping.name)
+ else
+ Devise.mailer_sender
+ end
+ end
+
+ def template_paths
+ template_path = [self.class.mailer_name]
+ template_path.unshift "#{@devise_mapping.scoped_path}/mailer" if self.class.scoped_views?
+ template_path
+ end
+
+ # 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
+ end
+ end
+end
@rymai

Just so you know, setup_mail was used by devise_invitable so it broke it, I'll pull-request devise_invitable to fix it right away.

@josevalim

Ugh, this is bad. I will add a deprecation warning to Devise as well.

Please sign in to comment.
Something went wrong with that request. Please try again.