From 8ac1a845f648b583820ea3665ea9e067f6ba6092 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Fri, 7 Jun 2024 15:38:10 +0100 Subject: [PATCH] Add stats for character sheets --- app/models/character_sheet_section.rb | 8 +++- app/models/stat.rb | 39 +++++++++++++++++++ db/migrate/20240607091417_create_stats.rb | 21 ++++++++++ db/schema.rb | 18 ++++++++- ...haracter_sheet_sections_controller_test.rb | 2 +- test/fixtures/character_sheet_sections.yml | 5 +++ test/fixtures/stats.yml | 6 +++ test/models/stat_test.rb | 25 ++++++++++++ 8 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 app/models/stat.rb create mode 100644 db/migrate/20240607091417_create_stats.rb create mode 100644 test/fixtures/stats.yml create mode 100644 test/models/stat_test.rb diff --git a/app/models/character_sheet_section.rb b/app/models/character_sheet_section.rb index 90ca354..e7f64e3 100644 --- a/app/models/character_sheet_section.rb +++ b/app/models/character_sheet_section.rb @@ -2,8 +2,12 @@ class CharacterSheetSection < ApplicationRecord belongs_to :character - belongs_to :parent_section, optional: true, class_name: "CharacterSheetSection" - has_many :character_sheet_subsections, class_name: "CharacterSheetSection", foreign_key: :parent_section_id + belongs_to :parent_section, optional: true, + class_name: "CharacterSheetSection" + has_many :character_sheet_subsections, class_name: "CharacterSheetSection", + foreign_key: :parent_section_id, + dependent: :destroy + has_many :stats, dependent: :destroy validates :name, presence: true, length: { maximum: 255 } diff --git a/app/models/stat.rb b/app/models/stat.rb new file mode 100644 index 0000000..3fdc7bf --- /dev/null +++ b/app/models/stat.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +class Stat < ApplicationRecord + belongs_to :character_sheet_section + + validates :name, presence: true, + length: { maximum: 100 }, + uniqueness: { scope: :character_sheet_section_id } + validates :slug, presence: true, + length: { maximum: 100 }, + uniqueness: true + validates :value, presence: true, + numericality: true + + before_validation :set_slug + + private + + def set_slug + return if slug.present? || name.blank? + + slug = if character_sheet_section.parent_section.present? + [ + character_sheet_section.parent_section.name.parameterize, + name.parameterize, + ].join("-") + else + slug = name.parameterize + end + + suffix = 2 + while Stat.exists?(slug:) + slug = "#{slug}-#{suffix}" + suffix += 1 + end + + self.slug = slug + end +end diff --git a/db/migrate/20240607091417_create_stats.rb b/db/migrate/20240607091417_create_stats.rb new file mode 100644 index 0000000..1368dc6 --- /dev/null +++ b/db/migrate/20240607091417_create_stats.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class CreateStats < ActiveRecord::Migration[7.1] + def change + create_table :stats do |t| + t.string :name, null: false + t.string :slug, null: false + t.belongs_to :character_sheet_section, null: false, foreign_key: true + t.decimal :value, null: false, default: 0 + t.string :roll_command + + t.timestamps + end + + add_check_constraint :stats, "length(name) <= 100", name: "chk_stat_name_max_length" + add_check_constraint :stats, "length(slug) <= 100", name: "chk_stat_slug_max_length" + + add_index :stats, :slug, unique: true + add_index :stats, [ :name, :character_sheet_section_id ], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index b62f771..d925b3b 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_175553) do +ActiveRecord::Schema[7.1].define(version: 2024_06_07_091417) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -209,6 +209,21 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_05_175553) do t.index ["key"], name: "index_solid_queue_semaphores_on_key", unique: true end + create_table "stats", force: :cascade do |t| + t.string "name", null: false + t.string "slug", null: false + t.bigint "character_sheet_section_id", null: false + t.decimal "value", default: "0.0", null: false + t.string "roll_command" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["character_sheet_section_id"], name: "index_stats_on_character_sheet_section_id" + t.index ["name", "character_sheet_section_id"], name: "index_stats_on_name_and_character_sheet_section_id", unique: true + t.index ["slug"], name: "index_stats_on_slug", unique: true + t.check_constraint "length(name::text) <= 100", name: "chk_stat_name_max_length" + t.check_constraint "length(slug::text) <= 100", name: "chk_stat_slug_max_length" + end + create_table "table_invites", force: :cascade do |t| t.bigint "table_id", null: false t.string "email", null: false @@ -269,6 +284,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_05_175553) do add_foreign_key "solid_queue_ready_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade add_foreign_key "solid_queue_recurring_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade add_foreign_key "solid_queue_scheduled_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade + add_foreign_key "stats", "character_sheet_sections" add_foreign_key "table_invites", "tables" add_foreign_key "tables", "game_systems" add_foreign_key "tables", "users", column: "owner_id" diff --git a/test/controllers/character_sheet_sections_controller_test.rb b/test/controllers/character_sheet_sections_controller_test.rb index e2aecd2..c004ffd 100644 --- a/test/controllers/character_sheet_sections_controller_test.rb +++ b/test/controllers/character_sheet_sections_controller_test.rb @@ -29,7 +29,7 @@ class CharacterSheetSectionsControllerTest < ActionDispatch::IntegrationTest user = users(:trevor) sign_in user assert_difference "CharacterSheetSection.count", -1 do - delete character_sheet_section_url(user.character_sheet_sections.first), + delete character_sheet_section_url(character_sheet_sections(:subsection)), as: :turbo_stream end end diff --git a/test/fixtures/character_sheet_sections.yml b/test/fixtures/character_sheet_sections.yml index 24e2d5a..5a657c9 100644 --- a/test/fixtures/character_sheet_sections.yml +++ b/test/fixtures/character_sheet_sections.yml @@ -1,3 +1,8 @@ stats: name: Stats character: nardren + +subsection: + name: Subsection + character: nardren + parent_section: stats diff --git a/test/fixtures/stats.yml b/test/fixtures/stats.yml new file mode 100644 index 0000000..6d9cb3c --- /dev/null +++ b/test/fixtures/stats.yml @@ -0,0 +1,6 @@ +strength: + name: Strength + slug: strength + character_sheet_section: stats + value: 10 + roll_command: d20+self diff --git a/test/models/stat_test.rb b/test/models/stat_test.rb new file mode 100644 index 0000000..f5e4860 --- /dev/null +++ b/test/models/stat_test.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "test_helper" + +class StatTest < ActiveSupport::TestCase + test "name must exist" do + assert_must_exist(stats(:strength), :name) + end + + test "value must exist" do + assert_must_exist(stats(:strength), :value) + end + + test "saving generates appropriate slug" do + stat = Stat.create(name: "Foo", character_sheet_section: character_sheet_sections(:subsection)) + assert_equal "stats-foo", stat.slug + end + + test "generates unique slug always" do + existing_stat = Stat.first + stat = Stat.create(name: existing_stat.name, character_sheet_section: existing_stat.character_sheet_section) + assert_not_equal existing_stat.slug, stat.slug + assert_equal "#{existing_stat.slug}-2", stat.slug + end +end