Add Player model
This commit is contained in:
parent
dd47a5f4bf
commit
e1c74ac2f5
|
@ -4,7 +4,7 @@ class TablesController < ApplicationController
|
||||||
before_action :set_table, only: [ :show, :edit, :update, :destroy ]
|
before_action :set_table, only: [ :show, :edit, :update, :destroy ]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@tables = Current.user.tables
|
@owned_tables = Current.user.owned_tables
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
@ -15,7 +15,7 @@ class TablesController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@table = Current.user.tables.new(table_params)
|
@table = Current.user.owned_tables.new(table_params)
|
||||||
if @table.save
|
if @table.save
|
||||||
redirect_to @table, notice: t(".success", name: @table.name)
|
redirect_to @table, notice: t(".success", name: @table.name)
|
||||||
else
|
else
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Player < ApplicationRecord
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :table
|
||||||
|
end
|
|
@ -3,6 +3,8 @@
|
||||||
class Table < ApplicationRecord
|
class Table < ApplicationRecord
|
||||||
belongs_to :owner, class_name: "User"
|
belongs_to :owner, class_name: "User"
|
||||||
belongs_to :game_system
|
belongs_to :game_system
|
||||||
|
has_many :players, dependent: :destroy
|
||||||
|
has_many :users, through: :players
|
||||||
|
|
||||||
validates :name, presence: true,
|
validates :name, presence: true,
|
||||||
length: { maximum: 100 },
|
length: { maximum: 100 },
|
||||||
|
@ -14,14 +16,21 @@ class Table < ApplicationRecord
|
||||||
|
|
||||||
before_validation :generate_uuid, if: :new_record?
|
before_validation :generate_uuid, if: :new_record?
|
||||||
before_validation :generate_slug
|
before_validation :generate_slug
|
||||||
|
after_create :add_first_player
|
||||||
|
|
||||||
def generate_uuid
|
private
|
||||||
self.uuid = SecureRandom.uuid
|
|
||||||
end
|
|
||||||
|
|
||||||
def generate_slug
|
def add_first_player
|
||||||
return if slug.present?
|
self.players.create(user: owner, table: self)
|
||||||
|
end
|
||||||
|
|
||||||
self.slug = "#{name.parameterize}-#{uuid}"
|
def generate_uuid
|
||||||
end
|
self.uuid = SecureRandom.uuid
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_slug
|
||||||
|
return if slug.present?
|
||||||
|
|
||||||
|
self.slug = "#{name.parameterize}-#{uuid}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
|
|
||||||
class User < ApplicationRecord
|
class User < ApplicationRecord
|
||||||
has_and_belongs_to_many :site_roles
|
has_and_belongs_to_many :site_roles
|
||||||
has_many :tables, foreign_key: :owner_id
|
has_many :owned_tables, foreign_key: :owner_id, class_name: "Table"
|
||||||
|
has_many :players, dependent: :destroy
|
||||||
|
has_many :tables, through: :players
|
||||||
|
|
||||||
has_secure_password
|
has_secure_password
|
||||||
generates_token_for :password_reset, expires_in: 4.hours do
|
generates_token_for :password_reset, expires_in: 4.hours do
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
<%= link_to t(".new_table"), new_table_path %>
|
<%= link_to t(".new_table"), new_table_path %>
|
||||||
|
|
||||||
<h2>Your tables</h2>
|
<h2>Tables you own</h2>
|
||||||
<% if @tables.any? %>
|
<% if @owned_tables.any? %>
|
||||||
<%= render @tables %>
|
<%= render @owned_tables %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<p>You have no tables.</p>
|
<p>You do not own any tables.</p>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class CreatePlayers < ActiveRecord::Migration[7.1]
|
||||||
|
def change
|
||||||
|
create_table :players, primary_key: [ :user_id, :table_id ] do |t|
|
||||||
|
t.belongs_to :user, null: false, foreign_key: true
|
||||||
|
t.belongs_to :table, null: false, foreign_key: true
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[7.1].define(version: 2024_05_28_192517) do
|
ActiveRecord::Schema[7.1].define(version: 2024_05_29_074949) do
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
|
||||||
|
@ -22,6 +22,15 @@ ActiveRecord::Schema[7.1].define(version: 2024_05_28_192517) do
|
||||||
t.check_constraint "length(name::text) <= 100", name: "chk_name_max_length"
|
t.check_constraint "length(name::text) <= 100", name: "chk_name_max_length"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "players", primary_key: ["user_id", "table_id"], force: :cascade do |t|
|
||||||
|
t.bigint "user_id", null: false
|
||||||
|
t.bigint "table_id", null: false
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["table_id"], name: "index_players_on_table_id"
|
||||||
|
t.index ["user_id"], name: "index_players_on_user_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "site_roles", force: :cascade do |t|
|
create_table "site_roles", force: :cascade do |t|
|
||||||
t.string "name", null: false
|
t.string "name", null: false
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
|
@ -67,6 +76,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_05_28_192517) do
|
||||||
t.check_constraint "length(username::text) >= 3", name: "chk_username_min_length"
|
t.check_constraint "length(username::text) >= 3", name: "chk_username_min_length"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
add_foreign_key "players", "tables"
|
||||||
|
add_foreign_key "players", "users"
|
||||||
add_foreign_key "tables", "game_systems"
|
add_foreign_key "tables", "game_systems"
|
||||||
add_foreign_key "tables", "users", column: "owner_id"
|
add_foreign_key "tables", "users", column: "owner_id"
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
one:
|
||||||
|
user: trevor
|
||||||
|
table: table
|
||||||
|
|
||||||
|
two:
|
||||||
|
user: gimli
|
||||||
|
table: table
|
|
@ -10,6 +10,11 @@ trevor:
|
||||||
first_name: Trevor
|
first_name: Trevor
|
||||||
last_name: Vallender
|
last_name: Vallender
|
||||||
|
|
||||||
|
gimli:
|
||||||
|
<<: *DEFAULTS
|
||||||
|
first_name: Gimli
|
||||||
|
last_name: son of Glóin
|
||||||
|
|
||||||
unverified:
|
unverified:
|
||||||
<<: *DEFAULTS
|
<<: *DEFAULTS
|
||||||
first_name: Unverified
|
first_name: Unverified
|
||||||
|
|
|
@ -25,4 +25,11 @@ class TableTest < ActiveSupport::TestCase
|
||||||
assert_not_nil table.slug
|
assert_not_nil table.slug
|
||||||
assert table.slug.start_with?("new-table")
|
assert table.slug.start_with?("new-table")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "creating adds creator as a player" do
|
||||||
|
table = Table.create(name: "New table", game_system: GameSystem.first, owner: User.first)
|
||||||
|
table.reload
|
||||||
|
assert_equal 1, table.players.count
|
||||||
|
assert_equal User.first, table.players.first.user
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue