Add stats for character sheets

This commit is contained in:
Trevor Vallender 2024-06-07 15:38:10 +01:00
parent 1afecfd781
commit 8ac1a845f6
8 changed files with 120 additions and 4 deletions

View File

@ -2,8 +2,12 @@
class CharacterSheetSection < ApplicationRecord class CharacterSheetSection < ApplicationRecord
belongs_to :character belongs_to :character
belongs_to :parent_section, optional: true, class_name: "CharacterSheetSection" belongs_to :parent_section, optional: true,
has_many :character_sheet_subsections, class_name: "CharacterSheetSection", foreign_key: :parent_section_id 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, validates :name, presence: true,
length: { maximum: 255 } length: { maximum: 255 }

39
app/models/stat.rb Normal file
View File

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

View File

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

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_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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" 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 t.index ["key"], name: "index_solid_queue_semaphores_on_key", unique: true
end 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| create_table "table_invites", force: :cascade do |t|
t.bigint "table_id", null: false t.bigint "table_id", null: false
t.string "email", 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_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_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 "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 "table_invites", "tables"
add_foreign_key "tables", "game_systems" add_foreign_key "tables", "game_systems"
add_foreign_key "tables", "users", column: "owner_id" add_foreign_key "tables", "users", column: "owner_id"

View File

@ -29,7 +29,7 @@ class CharacterSheetSectionsControllerTest < ActionDispatch::IntegrationTest
user = users(:trevor) user = users(:trevor)
sign_in user sign_in user
assert_difference "CharacterSheetSection.count", -1 do 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 as: :turbo_stream
end end
end end

View File

@ -1,3 +1,8 @@
stats: stats:
name: Stats name: Stats
character: nardren character: nardren
subsection:
name: Subsection
character: nardren
parent_section: stats

6
test/fixtures/stats.yml vendored Normal file
View File

@ -0,0 +1,6 @@
strength:
name: Strength
slug: strength
character_sheet_section: stats
value: 10
roll_command: d20+self

25
test/models/stat_test.rb Normal file
View File

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