tabletop-companion/app/models/user.rb

60 lines
1.8 KiB
Ruby

# frozen_string_literal: true
class User < ApplicationRecord
has_and_belongs_to_many :site_roles
has_many :owned_tables, foreign_key: :owner_id, class_name: "Table"
has_many :players, dependent: :destroy
has_many :tables, through: :players
has_rich_text :profile
has_one_attached :avatar do |attachable|
attachable.variant :standard, resize_to_limit: [ 100, 100 ], preprocessed: true
end
validates :avatar, content_type: /\Aimage\/.*\z/,
dimension: { width: { in: 10..1000 }, height: { in: 10..1000 } }
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 to_param
username
end
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