1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Small refactoring.

This commit is contained in:
José Valim 2009-10-20 08:44:21 -02:00
parent 59a9f38983
commit d505fd15c0
4 changed files with 17 additions and 18 deletions

View file

@ -21,7 +21,7 @@ class Notifier < ::ActionMailer::Base
recipients record.email
sent_on Time.now
content_type 'text/html'
body record.class.name.downcase.to_sym => record, :resource => record
body underscore_name(record) => record, :resource => record
end
# Setup subject namespaced by model. It means you're able to setup your
@ -36,8 +36,12 @@ class Notifier < ::ActionMailer::Base
# notifier:
# confirmation_instructions: '...'
def translate(record, key)
I18n.t(:"#{record.class.name.downcase}.#{key}",
I18n.t(:"#{underscore_name(record)}.#{key}",
:scope => [:devise, :notifier],
:default => key)
end
def underscore_name(record)
@underscore_name ||= record.class.name.underscore.to_sym
end
end

View file

@ -1,7 +1,7 @@
begin
require 'warden'
rescue
gem 'hassox-warden'
gem 'warden'
require 'warden'
end
@ -14,6 +14,8 @@ module Devise
:passwords => :recoverable,
:confirmations => :confirmable
}.freeze
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze
end
require 'devise/warden'

View file

@ -6,9 +6,8 @@
Warden::Manager.after_authentication do |record, auth, options|
scope = options[:scope]
remember_me = auth.params[scope].try(:fetch, :remember_me, nil)
remember_me = remember_me == '1' || remember_me == 'true'
mapping = Devise.mappings[scope]
if remember_me && mapping.present? && mapping.rememberable?
if Devise::TRUE_VALUES.include?(remember_me) && record.respond_to?(:remember_me!)
record.remember_me!
auth.cookies['remember_token'] = record.class.serialize_into_cookie(record)
end
@ -17,10 +16,8 @@ end
# 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
# 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|
mapping = Devise.mappings[scope]
if mapping.present? && mapping.rememberable?
if record.respond_to?(:forget_me!)
record.forget_me!
auth.cookies['remember_token'] = nil
end

View file

@ -13,8 +13,10 @@ module Devise
#
# User.find(1).remember_me! # regenerating the token
# User.find(1).forget_me! # clearing the token
#
# # generating info to put into cookies
# User.serialize_into_cookie(user)
#
# # lookup the user based on the incoming cookie information
# User.serialize_from_cookie(cookie_string)
module Rememberable
@ -51,14 +53,6 @@ module Devise
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
def serialize_into_cookie(record)
"#{record.id}::#{record.remember_token}"
@ -67,8 +61,10 @@ module Devise
# Recreate the user based on the stored cookie
def serialize_from_cookie(cookie)
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