lib/devise/models/authenticatable.rb in devise-0.6.3 vs lib/devise/models/authenticatable.rb in devise-0.7.0
- old
+ new
@@ -1,11 +1,24 @@
require 'devise/strategies/authenticatable'
require 'devise/serializers/authenticatable'
module Devise
module Models
+ module SessionSerializer
+ # Hook to serialize user into session. Overwrite if you want.
+ def serialize_into_session(record)
+ [record.class, record.id]
+ end
+ # Hook to serialize user from session. Overwrite if you want.
+ def serialize_from_session(keys)
+ klass, id = keys
+ raise "#{self} cannot serialize from #{klass} session since it's not its ancestors" unless klass <= self
+ klass.find(:first, :conditions => { :id => id })
+ end
+ end
+
# Authenticable Module, responsible for encrypting password and validating
# authenticity of a user while signing in.
#
# Configuration:
#
@@ -30,10 +43,11 @@
#
module Authenticatable
def self.included(base)
base.class_eval do
extend ClassMethods
+ extend SessionSerializer
attr_reader :password
attr_accessor :password_confirmation
end
end
@@ -61,27 +75,15 @@
end
module ClassMethods
# Authenticate a user based on configured attribute keys. Returns the
# authenticated user if it's valid or nil. Attributes are by default
- # :email and :password, the latter is always required.
+ # :email and :password, but the latter is always required.
def authenticate(attributes={})
return unless authentication_keys.all? { |k| attributes[k].present? }
conditions = attributes.slice(*authentication_keys)
resource = find_for_authentication(conditions)
valid_for_authentication(resource, attributes) if resource
- end
-
- # Hook to serialize user into session. Overwrite if you want.
- def serialize_into_session(record)
- [record.class, record.id]
- end
-
- # Hook to serialize user from session. Overwrite if you want.
- def serialize_from_session(keys)
- klass, id = keys
- raise "#{self} cannot serialize from #{klass} session since it's not its ancestors" unless klass <= self
- klass.find(:first, :conditions => { :id => id })
end
# Returns the class for the configured encryptor.
def encryptor_class
@encryptor_class ||= ::Devise::Encryptors.const_get(encryptor.to_s.classify)