28 lines
714 B
Ruby
28 lines
714 B
Ruby
# frozen_string_literal: true
|
|
|
|
class Table < ApplicationRecord
|
|
belongs_to :owner, class_name: "User"
|
|
belongs_to :game_system
|
|
|
|
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
|
|
|
|
def generate_uuid
|
|
self.uuid = SecureRandom.uuid
|
|
end
|
|
|
|
def generate_slug
|
|
return if slug.present?
|
|
|
|
self.slug = "#{name.parameterize}-#{uuid}"
|
|
end
|
|
end
|