diff --git a/app/controllers/tables_controller.rb b/app/controllers/tables_controller.rb index acc47d7..c949277 100644 --- a/app/controllers/tables_controller.rb +++ b/app/controllers/tables_controller.rb @@ -4,7 +4,7 @@ class TablesController < ApplicationController before_action :set_table, only: [ :show, :edit, :update, :destroy ] def index - @tables = Current.user.tables + @owned_tables = Current.user.owned_tables end def show @@ -15,7 +15,7 @@ class TablesController < ApplicationController end def create - @table = Current.user.tables.new(table_params) + @table = Current.user.owned_tables.new(table_params) if @table.save redirect_to @table, notice: t(".success", name: @table.name) else diff --git a/app/models/player.rb b/app/models/player.rb new file mode 100644 index 0000000..9189746 --- /dev/null +++ b/app/models/player.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class Player < ApplicationRecord + belongs_to :user + belongs_to :table +end diff --git a/app/models/table.rb b/app/models/table.rb index 0070e05..f3c1a04 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -3,6 +3,8 @@ class Table < ApplicationRecord belongs_to :owner, class_name: "User" belongs_to :game_system + has_many :players, dependent: :destroy + has_many :users, through: :players validates :name, presence: true, length: { maximum: 100 }, @@ -14,14 +16,21 @@ class Table < ApplicationRecord before_validation :generate_uuid, if: :new_record? before_validation :generate_slug + after_create :add_first_player - def generate_uuid - self.uuid = SecureRandom.uuid - end + private - def generate_slug - return if slug.present? + def add_first_player + self.players.create(user: owner, table: self) + end - self.slug = "#{name.parameterize}-#{uuid}" - end + def generate_uuid + self.uuid = SecureRandom.uuid + end + + def generate_slug + return if slug.present? + + self.slug = "#{name.parameterize}-#{uuid}" + end end diff --git a/app/models/user.rb b/app/models/user.rb index 619ea81..0247ab8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,7 +2,9 @@ class User < ApplicationRecord 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 generates_token_for :password_reset, expires_in: 4.hours do diff --git a/app/views/tables/index.html.erb b/app/views/tables/index.html.erb index c90911c..e4e17d4 100644 --- a/app/views/tables/index.html.erb +++ b/app/views/tables/index.html.erb @@ -2,9 +2,9 @@ <%= link_to t(".new_table"), new_table_path %> -

Your tables

-<% if @tables.any? %> - <%= render @tables %> +

Tables you own

+<% if @owned_tables.any? %> + <%= render @owned_tables %> <% else %> -

You have no tables.

+

You do not own any tables.

<% end %> diff --git a/db/migrate/20240529074949_create_players.rb b/db/migrate/20240529074949_create_players.rb new file mode 100644 index 0000000..083254d --- /dev/null +++ b/db/migrate/20240529074949_create_players.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 15c49c8..6a0e118 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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 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" 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| t.string "name", 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" end + add_foreign_key "players", "tables" + add_foreign_key "players", "users" add_foreign_key "tables", "game_systems" add_foreign_key "tables", "users", column: "owner_id" end diff --git a/test/fixtures/players.yml b/test/fixtures/players.yml new file mode 100644 index 0000000..8d8f5ac --- /dev/null +++ b/test/fixtures/players.yml @@ -0,0 +1,7 @@ +one: + user: trevor + table: table + +two: + user: gimli + table: table diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 355a319..7535296 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -10,6 +10,11 @@ trevor: first_name: Trevor last_name: Vallender +gimli: + <<: *DEFAULTS + first_name: Gimli + last_name: son of Glóin + unverified: <<: *DEFAULTS first_name: Unverified diff --git a/test/models/table_test.rb b/test/models/table_test.rb index d433aa0..c0d48b3 100644 --- a/test/models/table_test.rb +++ b/test/models/table_test.rb @@ -25,4 +25,11 @@ class TableTest < ActiveSupport::TestCase assert_not_nil table.slug assert table.slug.start_with?("new-table") 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 diff --git a/todo.md b/todo.md index 0284e99..018c317 100644 --- a/todo.md +++ b/todo.md @@ -1,4 +1,4 @@ - Add players to tables - - notifications +- notifications/table invites - Add characters to users/tables - Character sheets/prototypes