tabletop-companion/app/models/table.rb

40 lines
1.0 KiB
Ruby
Raw Permalink Normal View History

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-06-05 15:00:02 +00:00
has_many :characters, dependent: :nullify
2024-06-13 07:41:45 +00:00
has_many :dice_rolls, dependent: :destroy
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