40 lines
1.0 KiB
Ruby
40 lines
1.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Table < ApplicationRecord
|
|
belongs_to :owner, class_name: "User"
|
|
belongs_to :game_system
|
|
has_many :characters, dependent: :nullify
|
|
has_many :dice_rolls, dependent: :destroy
|
|
has_many :players, dependent: :destroy
|
|
has_many :table_invites, dependent: :destroy
|
|
has_many :users, through: :players
|
|
|
|
validates :name, presence: true,
|
|
length: { maximum: 100 },
|
|
uniqueness: { scope: :owner_id, message: I18n.t("errors.unique_table_name") }
|
|
validates :slug, presence: true,
|
|
length: { maximum: 100 },
|
|
uniqueness: true
|
|
validates :uuid, presence: true
|
|
|
|
before_validation :generate_uuid, if: :new_record?
|
|
before_validation :generate_slug
|
|
after_create :add_first_player
|
|
|
|
private
|
|
|
|
def add_first_player
|
|
self.players.create(user: owner, table: self)
|
|
end
|
|
|
|
def generate_uuid
|
|
self.uuid = SecureRandom.uuid
|
|
end
|
|
|
|
def generate_slug
|
|
return if slug.present?
|
|
|
|
self.slug = "#{name.parameterize}-#{uuid}"
|
|
end
|
|
end
|