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


105
106
107
108
109
110
# File 'lib/devise/models/authenticatable.rb', line 105

def find_for_authentication(conditions)
  conditions = filter_auth_params(conditions.dup)
  (case_insensitive_keys || []).each { |k| conditions[k].try(:downcase!) }
  (strip_whitespace_keys || []).each { |k| conditions[k].try(:strip!) }
  to_adapter.find_first(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.



113
114
115
# File 'lib/devise/models/authenticatable.rb', line 113

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.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/devise/models/authenticatable.rb', line 118

def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid) #:nodoc:
  (case_insensitive_keys || []).each { |k| attributes[k].try(:downcase!) }
  (strip_whitespace_keys || []).each { |k| attributes[k].try(:strip!) }

  attributes = attributes.slice(*required_attributes)
  attributes.delete_if { |key, value| value.blank? }

  if attributes.size == required_attributes.size
    record = to_adapter.find_first(filter_auth_params(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)


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

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

#params_authenticatable?(strategy) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/devise/models/authenticatable.rb', line 85

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