Module: Devise::Models::Authenticatable::ClassMethods

Defined in:
lib/devise/models/authenticatable.rb

Instance Method Summary collapse

Instance Method Details

#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
  super
end


92
93
94
# File 'lib/devise/models/authenticatable.rb', line 92

def find_for_authentication(conditions)
  find(:first, :conditions => conditions)
end

#find_or_initialize_with_error_by(attribute, value, error = :invalid) ⇒ Object

Find an initialize a record setting an error if it can’t be found.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/devise/models/authenticatable.rb', line 97

def find_or_initialize_with_error_by(attribute, value, error=:invalid) #:nodoc:
  if value.present?
    conditions = { attribute => value }
    record = find(:first, :conditions => conditions)
  end

  unless record
    record = new
    if value.present?
      record.send(:"#{attribute}=", value)
    else
      error = :blank
    end
    record.errors.add(attribute, error)
  end

  record
end

#generate_token(column) ⇒ Object

Generate a token by looping and ensuring does not already exist.



117
118
119
120
121
122
# File 'lib/devise/models/authenticatable.rb', line 117

def generate_token(column)
  loop do
    token = Devise.friendly_token
    break token unless find(:first, :conditions => { column => token })
  end
end

#http_authenticatable?(strategy) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/devise/models/authenticatable.rb', line 77

def http_authenticatable?(strategy)
  http_authenticatable.is_a?(Array) ?
    http_authenticatable.include?(strategy) : http_authenticatable
end

#params_authenticatable?(strategy) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/devise/models/authenticatable.rb', line 72

def params_authenticatable?(strategy)
  params_authenticatable.is_a?(Array) ?
    params_authenticatable.include?(strategy) : params_authenticatable
end