tabletop-companion/app/models/user.rb

68 lines
2.1 KiB
Ruby
Raw Permalink 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-06-04 13:20:57 +00:00
include DeletableAttachments
deletable_attachments :avatar
2024-05-26 09:06:04 +00:00
has_and_belongs_to_many :site_roles
2024-06-05 15:00:02 +00:00
has_many :characters, dependent: :destroy
has_many :character_sheet_sections, through: :characters
2024-06-17 16:12:19 +00:00
has_many :character_sheet_features, through: :character_sheet_sections
2024-05-29 08:09:58 +00:00
has_many :owned_tables, foreign_key: :owner_id, class_name: "Table"
has_many :players, dependent: :destroy
2024-06-10 19:39:46 +00:00
has_many :stats, through: :character_sheet_sections
2024-05-29 08:09:58 +00:00
has_many :tables, through: :players
2024-06-14 11:59:35 +00:00
has_many :text_fields, through: :character_sheet_sections
2024-05-30 08:07:54 +00:00
has_rich_text :profile
2024-05-30 14:30:25 +00:00
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 } }
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