From 701d69fcc943544c730f8c311d861e40ff08af74 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Wed, 5 Jun 2024 19:45:45 +0100 Subject: [PATCH] Add CharacterSheetSection model --- app/models/character.rb | 1 + app/models/character_sheet_section.rb | 12 ++++++++++++ ...40605175553_create_character_sheet_sections.rb | 15 +++++++++++++++ db/schema.rb | 15 ++++++++++++++- test/fixtures/character_sheet_sections.yml | 3 +++ test/models/character_sheet_section_test.rb | 9 +++++++++ todo.md | 1 + 7 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 app/models/character_sheet_section.rb create mode 100644 db/migrate/20240605175553_create_character_sheet_sections.rb create mode 100644 test/fixtures/character_sheet_sections.yml create mode 100644 test/models/character_sheet_section_test.rb diff --git a/app/models/character.rb b/app/models/character.rb index 9e0ca3f..8d60aa1 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -4,6 +4,7 @@ class Character < ApplicationRecord belongs_to :table, optional: true belongs_to :game_system belongs_to :user + has_many :character_sheet_sections, dependent: :destroy validates :name, presence: true, length: { maximum: 200 } diff --git a/app/models/character_sheet_section.rb b/app/models/character_sheet_section.rb new file mode 100644 index 0000000..7797aba --- /dev/null +++ b/app/models/character_sheet_section.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class CharacterSheetSection < ApplicationRecord + belongs_to :character + belongs_to :parent_section, optional: true + has_many :character_sheet_subsections, class_name: "CharacterSheetSection" + + validates :name, presence: true, + length: { maximum: 255 } + + scope :top_level, -> { where.missing(:parent_section) } +end diff --git a/db/migrate/20240605175553_create_character_sheet_sections.rb b/db/migrate/20240605175553_create_character_sheet_sections.rb new file mode 100644 index 0000000..f9133d2 --- /dev/null +++ b/db/migrate/20240605175553_create_character_sheet_sections.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class CreateCharacterSheetSections < ActiveRecord::Migration[7.1] + def change + create_table :character_sheet_sections do |t| + t.string :name, null: false + t.belongs_to :character, null: false, foreign_key: true + t.references :parent_section, null: true, foreign_key: { to_table: :character_sheet_sections } + + t.timestamps + end + + add_check_constraint :character_sheet_sections, "length(name) <= 255", name: "chk_character_sheet_section_name_max_length" + end +end diff --git a/db/schema.rb b/db/schema.rb index 217ec72..b62f771 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_06_05_143732) do +ActiveRecord::Schema[7.1].define(version: 2024_06_05_175553) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -52,6 +52,17 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_05_143732) do t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true end + create_table "character_sheet_sections", force: :cascade do |t| + t.string "name", null: false + t.bigint "character_id", null: false + t.bigint "parent_section_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["character_id"], name: "index_character_sheet_sections_on_character_id" + t.index ["parent_section_id"], name: "index_character_sheet_sections_on_parent_section_id" + t.check_constraint "length(name::text) <= 255", name: "chk_character_sheet_section_name_max_length" + end + create_table "characters", force: :cascade do |t| t.string "name", null: false t.bigint "table_id" @@ -245,6 +256,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_05_143732) 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 "character_sheet_sections", "character_sheet_sections", column: "parent_section_id" + add_foreign_key "character_sheet_sections", "characters" add_foreign_key "characters", "game_systems" add_foreign_key "characters", "tables" add_foreign_key "characters", "users" diff --git a/test/fixtures/character_sheet_sections.yml b/test/fixtures/character_sheet_sections.yml new file mode 100644 index 0000000..24e2d5a --- /dev/null +++ b/test/fixtures/character_sheet_sections.yml @@ -0,0 +1,3 @@ +stats: + name: Stats + character: nardren diff --git a/test/models/character_sheet_section_test.rb b/test/models/character_sheet_section_test.rb new file mode 100644 index 0000000..98a83e8 --- /dev/null +++ b/test/models/character_sheet_section_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require "test_helper" + +class CharacterSheetSectionTest < ActiveSupport::TestCase + test "name must exist" do + assert_must_exist(character_sheet_sections(:stats), "name") + end +end diff --git a/todo.md b/todo.md index 9dc7453..b997165 100644 --- a/todo.md +++ b/todo.md @@ -1,4 +1,5 @@ - default avatars +- add uuid/slug to characters and any other url-visible ids - shared/private notes - Add characters to users/tables - Character sheets/prototypes