Small refactoring.
This commit is contained in:
parent
59a9f38983
commit
d505fd15c0
|
@ -21,7 +21,7 @@ class Notifier < ::ActionMailer::Base
|
||||||
recipients record.email
|
recipients record.email
|
||||||
sent_on Time.now
|
sent_on Time.now
|
||||||
content_type 'text/html'
|
content_type 'text/html'
|
||||||
body record.class.name.downcase.to_sym => record, :resource => record
|
body underscore_name(record) => record, :resource => record
|
||||||
end
|
end
|
||||||
|
|
||||||
# Setup subject namespaced by model. It means you're able to setup your
|
# Setup subject namespaced by model. It means you're able to setup your
|
||||||
|
@ -36,8 +36,12 @@ class Notifier < ::ActionMailer::Base
|
||||||
# notifier:
|
# notifier:
|
||||||
# confirmation_instructions: '...'
|
# confirmation_instructions: '...'
|
||||||
def translate(record, key)
|
def translate(record, key)
|
||||||
I18n.t(:"#{record.class.name.downcase}.#{key}",
|
I18n.t(:"#{underscore_name(record)}.#{key}",
|
||||||
:scope => [:devise, :notifier],
|
:scope => [:devise, :notifier],
|
||||||
:default => key)
|
:default => key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def underscore_name(record)
|
||||||
|
@underscore_name ||= record.class.name.underscore.to_sym
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
begin
|
begin
|
||||||
require 'warden'
|
require 'warden'
|
||||||
rescue
|
rescue
|
||||||
gem 'hassox-warden'
|
gem 'warden'
|
||||||
require 'warden'
|
require 'warden'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -14,6 +14,8 @@ module Devise
|
||||||
:passwords => :recoverable,
|
:passwords => :recoverable,
|
||||||
:confirmations => :confirmable
|
:confirmations => :confirmable
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
|
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'devise/warden'
|
require 'devise/warden'
|
||||||
|
|
|
@ -6,9 +6,8 @@
|
||||||
Warden::Manager.after_authentication do |record, auth, options|
|
Warden::Manager.after_authentication do |record, auth, options|
|
||||||
scope = options[:scope]
|
scope = options[:scope]
|
||||||
remember_me = auth.params[scope].try(:fetch, :remember_me, nil)
|
remember_me = auth.params[scope].try(:fetch, :remember_me, nil)
|
||||||
remember_me = remember_me == '1' || remember_me == 'true'
|
|
||||||
mapping = Devise.mappings[scope]
|
if Devise::TRUE_VALUES.include?(remember_me) && record.respond_to?(:remember_me!)
|
||||||
if remember_me && mapping.present? && mapping.rememberable?
|
|
||||||
record.remember_me!
|
record.remember_me!
|
||||||
auth.cookies['remember_token'] = record.class.serialize_into_cookie(record)
|
auth.cookies['remember_token'] = record.class.serialize_into_cookie(record)
|
||||||
end
|
end
|
||||||
|
@ -17,10 +16,8 @@ end
|
||||||
# Before logout hook to forget the user in the given scope, only if rememberable
|
# Before logout hook to forget the user in the given scope, only if rememberable
|
||||||
# is activated for this scope. Also clear remember token to ensure the user
|
# is activated for this scope. Also clear remember token to ensure the user
|
||||||
# won't be remembered again.
|
# won't be remembered again.
|
||||||
# TODO: verify warden to call before_logout when @users are not loaded yet.
|
|
||||||
Warden::Manager.before_logout do |record, auth, scope|
|
Warden::Manager.before_logout do |record, auth, scope|
|
||||||
mapping = Devise.mappings[scope]
|
if record.respond_to?(:forget_me!)
|
||||||
if mapping.present? && mapping.rememberable?
|
|
||||||
record.forget_me!
|
record.forget_me!
|
||||||
auth.cookies['remember_token'] = nil
|
auth.cookies['remember_token'] = nil
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,8 +13,10 @@ module Devise
|
||||||
#
|
#
|
||||||
# User.find(1).remember_me! # regenerating the token
|
# User.find(1).remember_me! # regenerating the token
|
||||||
# User.find(1).forget_me! # clearing the token
|
# User.find(1).forget_me! # clearing the token
|
||||||
|
#
|
||||||
# # generating info to put into cookies
|
# # generating info to put into cookies
|
||||||
# User.serialize_into_cookie(user)
|
# User.serialize_into_cookie(user)
|
||||||
|
#
|
||||||
# # lookup the user based on the incoming cookie information
|
# # lookup the user based on the incoming cookie information
|
||||||
# User.serialize_from_cookie(cookie_string)
|
# User.serialize_from_cookie(cookie_string)
|
||||||
module Rememberable
|
module Rememberable
|
||||||
|
@ -51,14 +53,6 @@ module Devise
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
|
||||||
# Attempts to remember the user through it's id and remember_token.
|
|
||||||
# Returns the user if one is found and the token is valid, otherwise nil.
|
|
||||||
# Attributes must contain :id and :remember_token
|
|
||||||
def remember_me!(attributes={})
|
|
||||||
rememberable = find_by_id(attributes[:id])
|
|
||||||
rememberable if rememberable.try(:valid_remember_token?, attributes[:remember_token])
|
|
||||||
end
|
|
||||||
|
|
||||||
# Create the cookie key using the record id and remember_token
|
# Create the cookie key using the record id and remember_token
|
||||||
def serialize_into_cookie(record)
|
def serialize_into_cookie(record)
|
||||||
"#{record.id}::#{record.remember_token}"
|
"#{record.id}::#{record.remember_token}"
|
||||||
|
@ -67,8 +61,10 @@ module Devise
|
||||||
# Recreate the user based on the stored cookie
|
# Recreate the user based on the stored cookie
|
||||||
def serialize_from_cookie(cookie)
|
def serialize_from_cookie(cookie)
|
||||||
record_id, remember_token = cookie.split('::')
|
record_id, remember_token = cookie.split('::')
|
||||||
remember_me!(:id => record_id, :remember_token => remember_token)
|
record = find_by_id(attributes[:id])
|
||||||
|
record if record.try(:valid_remember_token?, remember_token)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue