diff --git a/app/models/table.rb b/app/models/table.rb new file mode 100644 index 0000000..4e07726 --- /dev/null +++ b/app/models/table.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class Table < ApplicationRecord + belongs_to :owner, class_name: "User" + belongs_to :game_system + + validates :name, presence: true, + length: { maximum: 100 } + validates :slug, presence: true, + length: { maximum: 100 }, + uniqueness: true +end diff --git a/db/migrate/20240528103921_create_tables.rb b/db/migrate/20240528103921_create_tables.rb new file mode 100644 index 0000000..b694935 --- /dev/null +++ b/db/migrate/20240528103921_create_tables.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class CreateTables < ActiveRecord::Migration[7.1] + def change + create_table :tables do |t| + t.string :name, null: false + t.string :slug, index: { unique: true }, null: false + t.references :owner, foreign_key: { to_table: :users } + t.belongs_to :game_system, null: false, foreign_key: true + + t.timestamps + end + + add_check_constraint :tables, "length(name) <= 100", name: "chk_table_name_max_length" + add_check_constraint :tables, "length(slug) <= 100", name: "chk_table_slug_max_length" + end +end diff --git a/db/schema.rb b/db/schema.rb index 7beccff..77e0da0 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_26_102908) do +ActiveRecord::Schema[7.1].define(version: 2024_05_28_103921) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -35,6 +35,20 @@ ActiveRecord::Schema[7.1].define(version: 2024_05_26_102908) do t.index ["user_id"], name: "index_site_roles_users_on_user_id" end + create_table "tables", force: :cascade do |t| + t.string "name", null: false + t.string "slug", null: false + t.bigint "owner_id" + t.bigint "game_system_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["game_system_id"], name: "index_tables_on_game_system_id" + t.index ["owner_id"], name: "index_tables_on_owner_id" + t.index ["slug"], name: "index_tables_on_slug", unique: true + t.check_constraint "length(name::text) <= 100", name: "chk_table_name_max_length" + t.check_constraint "length(slug::text) <= 100", name: "chk_table_slug_max_length" + end + create_table "users", force: :cascade do |t| t.string "username", limit: 20, null: false t.string "password_digest", limit: 200, null: false @@ -52,4 +66,6 @@ ActiveRecord::Schema[7.1].define(version: 2024_05_26_102908) do t.check_constraint "length(username::text) >= 3", name: "chk_username_min_length" end + add_foreign_key "tables", "game_systems" + add_foreign_key "tables", "users", column: "owner_id" end diff --git a/test/fixtures/game_systems.yml b/test/fixtures/game_systems.yml index 4f95233..ca79d64 100644 --- a/test/fixtures/game_systems.yml +++ b/test/fixtures/game_systems.yml @@ -1,2 +1,5 @@ troika: name: Troika + +dnd: + name: Dungeons & Dragons diff --git a/test/fixtures/tables.yml b/test/fixtures/tables.yml new file mode 100644 index 0000000..fd489dc --- /dev/null +++ b/test/fixtures/tables.yml @@ -0,0 +1,11 @@ +table: + name: My Table + slug: my-table + owner: trevor + game_system: troika + +table_two: + name: My Other Table + slug: my-other-table + owner: trevor + game_system: troika diff --git a/test/integration/admin_game_systems_integration_test.rb b/test/integration/admin_game_systems_integration_test.rb index 23ed0b7..763468b 100644 --- a/test/integration/admin_game_systems_integration_test.rb +++ b/test/integration/admin_game_systems_integration_test.rb @@ -59,7 +59,7 @@ class AdminGameSystemsIntegrationTest < ActionDispatch::IntegrationTest test "destroy should destroy game system" do sign_in users(:admin) assert_difference "GameSystem.count", -1 do - delete admin_game_system_path(game_systems(:troika)) + delete admin_game_system_path(game_systems(:dnd)) end assert_redirected_to admin_game_systems_path end diff --git a/test/models/table_test.rb b/test/models/table_test.rb new file mode 100644 index 0000000..51d5d31 --- /dev/null +++ b/test/models/table_test.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "test_helper" + +class TableTest < ActiveSupport::TestCase + test "name must exist" do + assert_must_exist(tables(:table), "name") + end + + test "slug must exist" do + assert_must_exist(tables(:table), "slug") + end + + test "slug must be unique" do + table1 = Table.first + table2 = Table.second + table1.slug = table2.slug + assert_not table1.valid? + end +end