48 lines
1.0 KiB
Ruby
48 lines
1.0 KiB
Ruby
# frozen_string_literal: true
|
|
class User < ApplicationRecord
|
|
has_secure_password
|
|
has_many :candidates, foreign_key: :recruiter_id
|
|
has_many :reviewer_to_quizzes
|
|
has_many :quizzes, through: :reviewer_to_quizzes
|
|
|
|
validates :email, presence: true, uniqueness: true
|
|
validates :name, presence: true
|
|
validates :role, presence: true
|
|
validates :reset_token, uniqueness: true, allow_nil: true
|
|
|
|
def setup_reset
|
|
gen_reset_token
|
|
save
|
|
end
|
|
|
|
# TODO: move to mixin: UserRoles
|
|
def admin?
|
|
'admin' == role
|
|
end
|
|
|
|
# TODO: move to mixin: UserRoles
|
|
def manager?
|
|
%w(admin manager).include? role
|
|
end
|
|
|
|
# TODO: move to mixin: UserRoles
|
|
def recruiter?
|
|
'recruiter' == role
|
|
end
|
|
|
|
# TODO: move to mixin: UserRoles
|
|
def reviewer?
|
|
'reviewer' == role
|
|
end
|
|
|
|
private
|
|
|
|
def gen_reset_token
|
|
loop do
|
|
self[:reset_token] = SecureRandom.urlsafe_base64(10)
|
|
self[:reset_timestamp] = DateTime.current
|
|
break unless User.exists?(reset_token: self[:reset_token])
|
|
end
|
|
end
|
|
end
|