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

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

Instance Method Summary collapse

Instance Method Details

#devise_parameter_filterObject (protected)



279
280
281
# File 'lib/devise/models/authenticatable.rb', line 279

def devise_parameter_filter
  @devise_parameter_filter ||= Devise::ParameterFilter.new(case_insensitive_keys, strip_whitespace_keys)
end

#find_first_by_auth_conditions(tainted_conditions, opts = {}) ⇒ Object



246
247
248
# File 'lib/devise/models/authenticatable.rb', line 246

def find_first_by_auth_conditions(tainted_conditions, opts={})
  to_adapter.find_first(devise_parameter_filter.filter(tainted_conditions).merge(opts))
end

#find_for_authentication(tainted_conditions) ⇒ Object

Find first record based on conditions given (ie by the sign in form). This method is always called during an authentication process but it may be wrapped as well. For instance, database authenticatable provides a ‘find_for_database_authentication` that wraps a call to this method. This allows you to customize both database authenticatable or the whole authenticate stack by customize `find_for_authentication.`

Overwrite to add customized conditions, create a join, or maybe use a namedscope to filter records while authenticating. Example:

def self.find_for_authentication(tainted_conditions)
  find_first_by_auth_conditions(tainted_conditions, :active => true)
end

Finally, notice that Devise also queries for users in other scenarios besides authentication, for example when retrieving an user to send an e-mail for password reset. In such cases, find_for_authentication is not called.



242
243
244
# File 'lib/devise/models/authenticatable.rb', line 242

def find_for_authentication(tainted_conditions)
  find_first_by_auth_conditions(tainted_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.



251
252
253
# File 'lib/devise/models/authenticatable.rb', line 251

def find_or_initialize_with_error_by(attribute, value, error=:invalid) #:nodoc:
  find_or_initialize_with_errors([attribute], { attribute => value }, error)
end

#find_or_initialize_with_errors(required_attributes, attributes, error = :invalid) ⇒ Object

Find an initialize a group of attributes based on a list of required attributes.



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/devise/models/authenticatable.rb', line 256

def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid) #:nodoc:
  attributes = attributes.slice(*required_attributes)
  attributes.delete_if { |key, value| value.blank? }

  if attributes.size == required_attributes.size
    record = find_first_by_auth_conditions(attributes)
  end

  unless record
    record = new

    required_attributes.each do |key|
      value = attributes[key]
      record.send("#{key}=", value)
      record.errors.add(key, value.present? ? error : :blank)
    end
  end

  record
end

#http_authenticatable?(strategy) ⇒ Boolean

Returns:

  • (Boolean)


218
219
220
221
# File 'lib/devise/models/authenticatable.rb', line 218

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

#params_authenticatable?(strategy) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
# File 'lib/devise/models/authenticatable.rb', line 213

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

#serialize_from_session(key, salt) ⇒ Object



208
209
210
211
# File 'lib/devise/models/authenticatable.rb', line 208

def serialize_from_session(key, salt)
  record = to_adapter.get(key)
  record if record && record.authenticatable_salt == salt
end

#serialize_into_session(record) ⇒ Object



204
205
206
# File 'lib/devise/models/authenticatable.rb', line 204

def serialize_into_session(record)
  [record.to_key, record.authenticatable_salt]
end