Class: OmniAuth::Identity::Models::ActiveRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
OmniAuth::Identity::Model, SecurePassword
Defined in:
lib/omniauth/identity/models/active_record.rb

Overview

Note:

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

Note:

This is an abstract class; inherit from it to create your user model.

ActiveRecord is an ORM for MySQL, PostgreSQL, and SQLite3:
https://guides.rubyonrails.org/active_record_basics.html

This class provides a base for OmniAuth Identity models using ActiveRecord,
including secure password handling and authentication key management.

Examples:

Usage

class User < OmniAuth::Identity::Models::ActiveRecord
  # Add your fields here, e.g.:
  # self.table_name = 'users'
  # has_many :posts
end

# Migration example:
# create_table :users do |t|
#   t.string :email, null: false
#   t.string :password_digest, null: false
#   t.timestamps
# 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

Constant Summary

Constants included from SecurePassword

SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED

Constants included from OmniAuth::Identity::Model

OmniAuth::Identity::Model::SCHEMA_ATTRIBUTES

Instance Attribute Summary

Attributes included from SecurePassword

#MAX_PASSWORD_LENGTH_ALLOWED

Attributes included from OmniAuth::Identity::Model

#SCHEMA_ATTRIBUTES

Class Method Summary collapse

Methods included from SecurePassword

included

Methods included from OmniAuth::Identity::Model

#auth_key, #auth_key=, #authenticate, included, #info, #uid

Class Method Details

.auth_key=(key) ⇒ void

This method returns an undefined value.

Sets the authentication key for the model and adds uniqueness validation.

Examples:

class User < OmniAuth::Identity::Models::ActiveRecord
  self.auth_key = :email
end

Parameters:

  • key (Symbol, String)

    the attribute to use as the authentication key



54
55
56
57
# File 'lib/omniauth/identity/models/active_record.rb', line 54

def self.auth_key=(key)
  super
  validates_uniqueness_of(key, case_sensitive: false)
end

.locate(search_hash) ⇒ ActiveRecord::Base?

Finds a record by the given search criteria.

If the model has a ‘provider’ column, it defaults to ‘identity’.

Examples:

User.locate(email: 'user@example.com')

Parameters:

  • search_hash (Hash)

    the attributes to search for

Returns:

  • (ActiveRecord::Base, nil)

    the first matching record or nil



68
69
70
71
# File 'lib/omniauth/identity/models/active_record.rb', line 68

def self.locate(search_hash)
  search_hash = search_hash.reverse_merge!("provider" => "identity") if column_names.include?("provider")
  where(search_hash).first
end