Module: Negroni::Models::Base::ClassMethods

Defined in:
lib/negroni/models/base.rb

Overview

Class Methods for the ‘Base` module.

Callbacks collapse

Instance Method Details

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

Find the first record, given a set of ‘tainted_conditions`, and options.

Parameters:

  • tainted_conditions (Hash, ActionDispatch::Parameters)

    the conditions used to find the record

  • options (Hash) (defaults to: {})

    additional conditions and options for the find operation

Returns:

  • (Object)

    the first record returned from the database



245
246
247
248
249
# File 'lib/negroni/models/base.rb', line 245

def find_first_by_auth_conditions(tainted_conditions, options = {})
  to_adapter.find_first(
    _param_filter.filter(tainted_conditions).merge(options)
  )
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 authenticable provides a ‘find_for_database_authentication` that wraps a call to this method. This allows you to customize both database authenticable 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.

Finally, notice that Negroni 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.

Examples:

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


232
233
234
# File 'lib/negroni/models/base.rb', line 232

def find_for_authentication(tainted_conditions)
  find_first_by_auth_conditions(tainted_conditions)
end

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

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



252
253
254
# File 'lib/negroni/models/base.rb', line 252

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

#find_or_initialize_with_errors(required, attrs, error = :invalid) ⇒ Object

Find or initialize a record with a group of attributes, based on a list of required attributes



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

def find_or_initialize_with_errors(required, attrs, error = :invalid)
  attrs = _indifferently(required, attrs).delete_if { |_, v| v.blank? }

  if attrs.size == required.size
    record = find_first_by_auth_conditions(attrs)
  end

  unless record
    record = new

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

  record
end

#from_token_request(request) ⇒ Object?

Finds an entity of the including class by a token auth request. This allows users to sign in with either an email address or thier username.

Parameters:

  • request (Request)

    The request which contains the sign in params.

Returns:

  • (Object, nil)

    The found entity, or ‘nil` if one was not found.



285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/negroni/models/base.rb', line 285

def from_token_request(request)
  # Bail if there is no `auth` param
  return nil unless (auth_params = request.params['auth'])

  # find_first_by_auth_conditions(auth_params)

  authentication_keys.each do |key|
    if (found_key = auth_params[key.to_s])
      return to_adapter.find_first(key => found_key)
    end
  end
end