tabletop-companion/app/models/user.rb

55 lines
1.5 KiB
Ruby
Raw Normal View History

2024-05-26 10:45:10 +00:00
# frozen_string_literal: true
2024-04-14 10:10:10 +00:00
class User < ApplicationRecord
2024-05-26 09:06:04 +00:00
has_and_belongs_to_many :site_roles
2024-05-29 08:09:58 +00:00
has_many :owned_tables, foreign_key: :owner_id, class_name: "Table"
has_many :players, dependent: :destroy
has_many :tables, through: :players
2024-05-30 08:07:54 +00:00
has_rich_text :profile
2024-05-26 09:06:04 +00:00
2024-04-14 10:10:10 +00:00
has_secure_password
generates_token_for :password_reset, expires_in: 4.hours do
password_salt.last(10) # Invalidates when password changed
end
2024-04-14 19:01:32 +00:00
generates_token_for :email_verification, expires_in: 1.week do
verified.to_s
end
2024-04-14 10:10:10 +00:00
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,
2024-04-14 19:01:32 +00:00
message: I18n.t("users.validations.email_format"),
2024-04-14 10:48:42 +00:00
}
2024-04-14 10:10:10 +00:00
normalizes :email, with: ->(email) { email.strip.downcase }
validates :first_name,
presence: true,
length: { maximum: 50 }
validates :last_name,
allow_nil: false,
length: { maximum: 50 }
2024-04-14 19:01:32 +00:00
scope :verified, -> { where(verified: true) }
scope :unverified, -> { where(verified: false) }
2024-05-29 16:17:06 +00:00
def to_param
username
end
2024-04-14 10:10:10 +00:00
def full_name
return first_name if last_name.blank?
"#{first_name} #{last_name}"
end
2024-05-26 09:06:04 +00:00
def admin?
site_roles.include? SiteRole.find_by(name: "Admin")
end
2024-04-14 10:10:10 +00:00
end