gitnub/app/models/user.rb

56 lines
1.9 KiB
Ruby
Raw Normal View History

2021-09-22 16:35:54 +00:00
# frozen_string_literal: true
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# confirmation_sent_at :datetime
# confirmation_token :string
# confirmed_at :datetime
# current_sign_in_at :datetime
# current_sign_in_ip :string
# email :string default(""), not null
# encrypted_password :string default(""), not null
# last_sign_in_at :datetime
# last_sign_in_ip :string
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string
# sign_in_count :integer default(0), not null
# unconfirmed_email :string
# username :string default(""), not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_confirmation_token (confirmation_token) UNIQUE
# index_users_on_email (email) UNIQUE
# index_users_on_reset_password_token (reset_password_token) UNIQUE
# index_users_on_username (username) UNIQUE
#
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:confirmable, :trackable
attr_writer :login
def login
@login || username || email
end
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login == conditions.delete(:login)
where(conditions.to_h).where(['lower(username) = :value OR lower(email) = :value',
{ value: login.downcase }]).first
elsif conditions.key?(:username) || conditions.key?(:email)
where(conditions.to_h).first
end
end
end