Module: OmniAuth::Identity::Models::NoBrainer

Defined in:
lib/omniauth/identity/models/nobrainer.rb

Overview

Note:

NoBrainer is based on ActiveModel, so validations are enabled by default.

NoBrainer is an ORM adapter for RethinkDB: http://nobrainer.io/

This module provides OmniAuth Identity functionality for NoBrainer models,
including secure password handling and authentication key management.

Examples:

Usage

class User
  include NoBrainer::Document

  include OmniAuth::Identity::Models::NoBrainer

  field :email
  field :password_digest
end

user = User.new(email: 'user@example.com', password: 'password')
user.save

# Authenticate a user
authenticated_user = User.locate(email: 'user@example.com')
authenticated_user.authenticate('password') # => user or false

Class Method Summary collapse

Class Method Details

.included(base) ⇒ void

This method returns an undefined value.

Called when this module is included in a model class.

This method extends the base class with OmniAuth Identity functionality,
including secure password support and authentication key validation.

Parameters:

  • base (Class)

    the model class including this module



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/omniauth/identity/models/nobrainer.rb', line 39

def self.included(base)
  base.class_eval do
    include(::OmniAuth::Identity::Model)
    include(::OmniAuth::Identity::SecurePassword)

    # validations: true (default) incurs a dependency on ActiveModel, but NoBrainer is ActiveModel based.
    has_secure_password

    # @!method self.auth_key=(key)
    # Sets the authentication key for the model and adds uniqueness validation.
    #
    # @param key [Symbol, String] the attribute to use as the authentication key
    # @return [void]
    # @example
    #   class User
    #     include OmniAuth::Identity::Models::NoBrainer
    #     self.auth_key = :email
    #   end
    def self.auth_key=(key)
      super
      validates_uniqueness_of(key, case_sensitive: false)
    end

    # @!method self.locate(search_hash)
    # Finds a record by the given search criteria.
    #
    # @param search_hash [Hash] the attributes to search for
    # @return [Object, nil] the first matching record or nil
    # @example
    #   User.locate(email: 'user@example.com')
    def self.locate(search_hash)
      where(search_hash).first
    end
  end
end