From 05ffcf04560a24d61de1fc834580cff169bc7662 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Wed, 12 Jun 2024 16:17:27 +0100 Subject: [PATCH] Add counters --- app/models/character_sheet_section.rb | 1 + app/models/counter.rb | 14 ++++++++++++++ db/migrate/20240612145451_create_counters.rb | 20 ++++++++++++++++++++ db/schema.rb | 19 +++++++++++++++++-- test/fixtures/character_sheet_sections.yml | 4 ++++ test/fixtures/counters.yml | 5 +++++ test/models/counter_test.rb | 13 +++++++++++++ 7 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 app/models/counter.rb create mode 100644 db/migrate/20240612145451_create_counters.rb create mode 100644 test/fixtures/counters.yml create mode 100644 test/models/counter_test.rb diff --git a/app/models/character_sheet_section.rb b/app/models/character_sheet_section.rb index e7f64e3..657962c 100644 --- a/app/models/character_sheet_section.rb +++ b/app/models/character_sheet_section.rb @@ -7,6 +7,7 @@ class CharacterSheetSection < ApplicationRecord has_many :character_sheet_subsections, class_name: "CharacterSheetSection", foreign_key: :parent_section_id, dependent: :destroy + has_many :counters, dependent: :destroy has_many :stats, dependent: :destroy validates :name, presence: true, diff --git a/app/models/counter.rb b/app/models/counter.rb new file mode 100644 index 0000000..be3c17e --- /dev/null +++ b/app/models/counter.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class Counter < ApplicationRecord + include Sluggable + + belongs_to :character_sheet_section + + validates :name, presence: true, + length: { maximum: 100 }, + uniqueness: { scope: :character_sheet_section_id } + validates :value, presence: true, + numericality: true + before_validation :set_slug +end diff --git a/db/migrate/20240612145451_create_counters.rb b/db/migrate/20240612145451_create_counters.rb new file mode 100644 index 0000000..e70864c --- /dev/null +++ b/db/migrate/20240612145451_create_counters.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class CreateCounters < ActiveRecord::Migration[7.1] + def change + create_table :counters do |t| + t.string :name, null: false + t.string :slug, null: false + t.integer :value, null: false, default: 0 + t.belongs_to :character_sheet_section, null: false, foreign_key: true + + t.timestamps + end + + add_check_constraint :counters, "length(name) <= 100", name: "chk_counter_name_max_length" + add_check_constraint :counters, "length(slug) <= 100", name: "chk_counter_slug_max_length" + + add_index :counters, :slug, unique: true + add_index :counters, [ :name, :character_sheet_section_id ], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index d925b3b..4d930f7 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_07_091417) do +ActiveRecord::Schema[7.1].define(version: 2024_06_12_145451) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -76,6 +76,20 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_07_091417) do t.check_constraint "length(name::text) <= 200", name: "chk_character_name_max_length" end + create_table "counters", force: :cascade do |t| + t.string "name", null: false + t.string "slug", null: false + t.integer "value", default: 0, null: false + t.bigint "character_sheet_section_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["character_sheet_section_id"], name: "index_counters_on_character_sheet_section_id" + t.index ["name", "character_sheet_section_id"], name: "index_counters_on_name_and_character_sheet_section_id", unique: true + t.index ["slug"], name: "index_counters_on_slug", unique: true + t.check_constraint "length(name::text) <= 100", name: "chk_counter_name_max_length" + t.check_constraint "length(slug::text) <= 100", name: "chk_counter_slug_max_length" + end + create_table "game_systems", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false @@ -213,7 +227,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_07_091417) do 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.integer "value", default: 0, null: false t.string "roll_command" t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -276,6 +290,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_07_091417) do add_foreign_key "characters", "game_systems" add_foreign_key "characters", "tables" add_foreign_key "characters", "users" + add_foreign_key "counters", "character_sheet_sections" 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 diff --git a/test/fixtures/character_sheet_sections.yml b/test/fixtures/character_sheet_sections.yml index 5a657c9..a777fd8 100644 --- a/test/fixtures/character_sheet_sections.yml +++ b/test/fixtures/character_sheet_sections.yml @@ -2,6 +2,10 @@ stats: name: Stats character: nardren +counters: + name: Status + character: nardren + subsection: name: Subsection character: nardren diff --git a/test/fixtures/counters.yml b/test/fixtures/counters.yml new file mode 100644 index 0000000..1c83932 --- /dev/null +++ b/test/fixtures/counters.yml @@ -0,0 +1,5 @@ +hp: + name: HP + value: 10 + slug: hp + character_sheet_section: counters diff --git a/test/models/counter_test.rb b/test/models/counter_test.rb new file mode 100644 index 0000000..8da615e --- /dev/null +++ b/test/models/counter_test.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require "test_helper" + +class CounterTest < ActiveSupport::TestCase + test "name must exist" do + assert_must_exist(counters(:hp), :name) + end + + test "value must exist" do + assert_must_exist(counters(:hp), :value) + end +end