Module: Devise::Models::Authenticatable::ClassMethods
- Defined in:
- lib/devise/models/authenticatable.rb
Instance Method Summary collapse
-
#authenticate(attributes = {}) ⇒ Object
Authenticate a user based on configured attribute keys.
-
#encryptor_class ⇒ Object
Returns the class for the configured encryptor.
-
#find_for_authentication(conditions) ⇒ Object
Find first record based on conditions given (ie by the sign in form).
-
#find_or_initialize_with_error_by_email(email) ⇒ Object
Attempt to find a user by it’s email.
-
#serialize_from_session(keys) ⇒ Object
Hook to serialize user from session.
-
#serialize_into_session(record) ⇒ Object
Hook to serialize user into session.
Instance Method Details
#authenticate(attributes = {}) ⇒ Object
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.
66 67 68 69 70 71 |
# File 'lib/devise/models/authenticatable.rb', line 66 def authenticate(attributes={}) return unless authentication_keys.all? { |k| attributes[k].present? } conditions = attributes.slice(*authentication_keys) authenticatable = find_for_authentication(conditions) authenticatable if authenticatable.try(:valid_password?, attributes[:password]) end |
#encryptor_class ⇒ Object
Returns the class for the configured encryptor.
109 110 111 |
# File 'lib/devise/models/authenticatable.rb', line 109 def encryptor_class @encryptor_class ||= ::Devise::Encryptors.const_get(encryptor.to_s.classify) end |
#find_for_authentication(conditions) ⇒ Object
Find first record based on conditions given (ie by the sign in form). Overwrite to add customized conditions, create a join, or maybe use a namedscope to filter records while authenticating. Example:
def self.find_for_authentication(conditions={})
conditions[:active] = true
find(:first, :conditions => conditions)
end
83 84 85 |
# File 'lib/devise/models/authenticatable.rb', line 83 def find_for_authentication(conditions) find(:first, :conditions => conditions) end |
#find_or_initialize_with_error_by_email(email) ⇒ Object
Attempt to find a user by it’s email. If not user is found, returns a new user with an email not found error.
89 90 91 92 93 94 |
# File 'lib/devise/models/authenticatable.rb', line 89 def find_or_initialize_with_error_by_email(email) attributes = { :email => email } record = find(:first, :conditions => attributes) || new(attributes) record.errors.add(:email, :not_found, :default => 'not found') if record.new_record? record end |
#serialize_from_session(keys) ⇒ Object
Hook to serialize user from session. Overwrite if you want.
102 103 104 105 106 |
# File 'lib/devise/models/authenticatable.rb', line 102 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 |
#serialize_into_session(record) ⇒ Object
Hook to serialize user into session. Overwrite if you want.
97 98 99 |
# File 'lib/devise/models/authenticatable.rb', line 97 def serialize_into_session(record) [record.class, record.id] end |