2024-05-28 11:08:19 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Table < ApplicationRecord
|
|
|
|
belongs_to :owner, class_name: "User"
|
|
|
|
belongs_to :game_system
|
2024-05-29 08:09:58 +00:00
|
|
|
has_many :players, dependent: :destroy
|
2024-05-29 14:24:18 +00:00
|
|
|
has_many :table_invites, dependent: :destroy
|
2024-05-29 08:09:58 +00:00
|
|
|
has_many :users, through: :players
|
2024-05-28 11:08:19 +00:00
|
|
|
|
|
|
|
validates :name, presence: true,
|
2024-05-28 19:22:08 +00:00
|
|
|
length: { maximum: 100 },
|
|
|
|
uniqueness: { scope: :owner_id, message: I18n.t("errors.unique_table_name") }
|
2024-05-28 11:08:19 +00:00
|
|
|
validates :slug, presence: true,
|
|
|
|
length: { maximum: 100 },
|
|
|
|
uniqueness: true
|
2024-05-28 19:40:51 +00:00
|
|
|
validates :uuid, presence: true
|
|
|
|
|
|
|
|
before_validation :generate_uuid, if: :new_record?
|
|
|
|
before_validation :generate_slug
|
2024-05-29 08:09:58 +00:00
|
|
|
after_create :add_first_player
|
2024-05-28 19:40:51 +00:00
|
|
|
|
2024-05-29 08:09:58 +00:00
|
|
|
private
|
2024-05-28 19:40:51 +00:00
|
|
|
|
2024-05-29 08:09:58 +00:00
|
|
|
def add_first_player
|
|
|
|
self.players.create(user: owner, table: self)
|
|
|
|
end
|
2024-05-28 19:40:51 +00:00
|
|
|
|
2024-05-29 08:09:58 +00:00
|
|
|
def generate_uuid
|
|
|
|
self.uuid = SecureRandom.uuid
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_slug
|
|
|
|
return if slug.present?
|
|
|
|
|
|
|
|
self.slug = "#{name.parameterize}-#{uuid}"
|
|
|
|
end
|
2024-05-28 11:08:19 +00:00
|
|
|
end
|