tabletop-companion/app/models/user.rb

45 lines
1.3 KiB
Ruby

class User < ApplicationRecord
has_and_belongs_to_many :site_roles
has_secure_password
generates_token_for :password_reset, expires_in: 4.hours do
password_salt.last(10) # Invalidates when password changed
end
generates_token_for :email_verification, expires_in: 1.week do
verified.to_s
end
validates :username,
presence: true,
uniqueness: true,
length: { minimum: 3, maximum: 20 }
normalizes :username, with: ->(username) { username.strip.downcase }
validates :email,
presence: true,
uniqueness: true,
length: { minimum: 5, maximum: 100 },
format: { with: URI::MailTo::EMAIL_REGEXP,
message: I18n.t("users.validations.email_format"),
}
normalizes :email, with: ->(email) { email.strip.downcase }
validates :first_name,
presence: true,
length: { maximum: 50 }
validates :last_name,
allow_nil: false,
length: { maximum: 50 }
scope :verified, -> { where(verified: true) }
scope :unverified, -> { where(verified: false) }
def full_name
return first_name if last_name.blank?
"#{first_name} #{last_name}"
end
def admin?
site_roles.include? SiteRole.find_by(name: "Admin")
end
end