Add table model

This commit is contained in:
Trevor Vallender 2024-05-28 12:08:19 +01:00
parent fa802b81c5
commit 7fadc2d90b
7 changed files with 81 additions and 2 deletions

12
app/models/table.rb Normal file
View File

@ -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

View File

@ -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

18
db/schema.rb generated
View File

@ -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_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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" 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" t.index ["user_id"], name: "index_site_roles_users_on_user_id"
end 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| create_table "users", force: :cascade do |t|
t.string "username", limit: 20, null: false t.string "username", limit: 20, null: false
t.string "password_digest", limit: 200, 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" t.check_constraint "length(username::text) >= 3", name: "chk_username_min_length"
end end
add_foreign_key "tables", "game_systems"
add_foreign_key "tables", "users", column: "owner_id"
end end

View File

@ -1,2 +1,5 @@
troika: troika:
name: Troika name: Troika
dnd:
name: Dungeons & Dragons

11
test/fixtures/tables.yml vendored Normal file
View File

@ -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

View File

@ -59,7 +59,7 @@ class AdminGameSystemsIntegrationTest < ActionDispatch::IntegrationTest
test "destroy should destroy game system" do test "destroy should destroy game system" do
sign_in users(:admin) sign_in users(:admin)
assert_difference "GameSystem.count", -1 do assert_difference "GameSystem.count", -1 do
delete admin_game_system_path(game_systems(:troika)) delete admin_game_system_path(game_systems(:dnd))
end end
assert_redirected_to admin_game_systems_path assert_redirected_to admin_game_systems_path
end end

20
test/models/table_test.rb Normal file
View File

@ -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