Add Character model

This commit is contained in:
Trevor Vallender 2024-06-05 16:00:02 +01:00
parent 6aa9641fbe
commit 2aed4cdfc2
12 changed files with 95 additions and 2 deletions

10
app/models/character.rb Normal file
View File

@ -0,0 +1,10 @@
# frozen_string_literal: true
class Character < ApplicationRecord
belongs_to :table, optional: true
belongs_to :game_system
belongs_to :user
validates :name, presence: true,
length: { maximum: 200 }
end

View File

@ -1,6 +1,9 @@
# frozen_string_literal: true
class GameSystem < ApplicationRecord
has_many :characters, dependent: :restrict_with_error
has_many :tables, dependent: :restrict_with_error
validates :name, presence: true,
uniqueness: true,
length: { maximum: 100 }

View File

@ -3,6 +3,7 @@
class Table < ApplicationRecord
belongs_to :owner, class_name: "User"
belongs_to :game_system
has_many :characters, dependent: :nullify
has_many :players, dependent: :destroy
has_many :table_invites, dependent: :destroy
has_many :users, through: :players

View File

@ -5,6 +5,7 @@ class User < ApplicationRecord
deletable_attachments :avatar
has_and_belongs_to_many :site_roles
has_many :characters, dependent: :destroy
has_many :owned_tables, foreign_key: :owner_id, class_name: "Table"
has_many :players, dependent: :destroy
has_many :tables, through: :players

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
class CreateCharacters < ActiveRecord::Migration[7.1]
def change
create_table :characters do |t|
t.string :name, null: false
t.belongs_to :table, null: true, foreign_key: true
t.belongs_to :game_system, null: false, foreign_key: true
t.belongs_to :user, null: false, foreign_key: true
t.timestamps
end
add_check_constraint :characters, "length(name) <= 200", name: "chk_character_name_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.
ActiveRecord::Schema[7.1].define(version: 2024_06_05_132327) do
ActiveRecord::Schema[7.1].define(version: 2024_06_05_143732) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -52,6 +52,19 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_05_132327) do
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end
create_table "characters", force: :cascade do |t|
t.string "name", null: false
t.bigint "table_id"
t.bigint "game_system_id", null: false
t.bigint "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["game_system_id"], name: "index_characters_on_game_system_id"
t.index ["table_id"], name: "index_characters_on_table_id"
t.index ["user_id"], name: "index_characters_on_user_id"
t.check_constraint "length(name::text) <= 200", name: "chk_character_name_max_length"
end
create_table "game_systems", force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at", null: false
@ -232,6 +245,9 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_05_132327) do
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "characters", "game_systems"
add_foreign_key "characters", "tables"
add_foreign_key "characters", "users"
add_foreign_key "players", "tables"
add_foreign_key "players", "users"
add_foreign_key "solid_queue_blocked_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade

5
test/fixtures/characters.yml vendored Normal file
View File

@ -0,0 +1,5 @@
nardren:
name: Nardren Rockseeker
table: dnd_table
game_system: dnd
user: trevor

View File

@ -3,3 +3,6 @@ troika:
dnd:
name: Dungeons & Dragons
dragonbane:
name: Dragonbane

View File

@ -16,3 +16,8 @@ gimlis_table:
<<: *DEFAULTS
name: Gimli's Table
owner: gimli
dnd_table:
<<: *DEFAULTS
name: The Dungeon Master's Table
game_system: dnd

View File

@ -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(:dnd))
delete admin_game_system_path(game_systems(:dragonbane))
end
assert_redirected_to admin_game_systems_path
end

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
require "test_helper"
class CharacterTest < ActiveSupport::TestCase
test "name must exist" do
assert_must_exist(characters(:nardren), :name)
end
end

View File

@ -6,4 +6,28 @@ class GameSystemTest < ActiveSupport::TestCase
test "name must exist" do
assert_must_exist(game_systems(:troika), :name)
end
test "cannot delete games with characters" do
game = game_systems(:dnd)
game.tables.destroy_all
assert_no_difference("GameSystem.count") do
game_systems(:dnd).destroy
end
game.characters.destroy_all
assert_difference("GameSystem.count", -1) do
game_systems(:dnd).destroy
end
end
test "cannot delete games with tables" do
game = game_systems(:dnd)
game.characters.destroy_all
assert_no_difference("GameSystem.count") do
game_systems(:dnd).destroy
end
game.tables.destroy_all
assert_difference("GameSystem.count", -1) do
game_systems(:dnd).destroy
end
end
end