2024-06-05 18:45:45 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CharacterSheetSection < ApplicationRecord
|
|
|
|
belongs_to :character
|
2024-06-07 14:38:10 +00:00
|
|
|
belongs_to :parent_section, optional: true,
|
|
|
|
class_name: "CharacterSheetSection"
|
|
|
|
has_many :character_sheet_subsections, class_name: "CharacterSheetSection",
|
|
|
|
foreign_key: :parent_section_id,
|
|
|
|
dependent: :destroy
|
2024-06-17 13:53:09 +00:00
|
|
|
has_many :character_sheet_features
|
|
|
|
has_many :stats, dependent: :destroy, through: :character_sheet_features,
|
|
|
|
source: :featurable, source_type: "Stat"
|
|
|
|
has_many :text_fields, dependent: :destroy, through: :character_sheet_features,
|
|
|
|
source: :featurable, source_type: "TextField"
|
2024-06-05 18:45:45 +00:00
|
|
|
|
|
|
|
validates :name, presence: true,
|
|
|
|
length: { maximum: 255 }
|
|
|
|
|
|
|
|
scope :top_level, -> { where.missing(:parent_section) }
|
2024-06-21 10:22:25 +00:00
|
|
|
|
2024-07-03 16:28:09 +00:00
|
|
|
def self.new_from_template(template:, character:, params:)
|
|
|
|
section = CharacterSheetSection.deserialize(JSON.parse(template.content))
|
|
|
|
section.assign_attributes(params)
|
|
|
|
section.character = character
|
|
|
|
section
|
|
|
|
end
|
|
|
|
|
2024-06-21 10:22:25 +00:00
|
|
|
def lowest_order_index
|
|
|
|
character_sheet_features.minimum(:order_index) || 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def highest_order_index
|
|
|
|
character_sheet_features.maximum(:order_index) || 1
|
|
|
|
end
|
2024-06-30 08:14:55 +00:00
|
|
|
|
|
|
|
def self.deserialize(h)
|
|
|
|
section = new
|
|
|
|
|
|
|
|
h["character_sheet_subsections"].each do |sub|
|
|
|
|
section.character_sheet_subsections.deserialize(sub)
|
|
|
|
end
|
|
|
|
|
|
|
|
h["stats"].each do |stat|
|
|
|
|
section.stats.build(stat)
|
|
|
|
end
|
|
|
|
|
|
|
|
h["text_fields"].each do |text_field|
|
|
|
|
section.text_fields.build(text_field)
|
|
|
|
end
|
|
|
|
|
|
|
|
section
|
|
|
|
end
|
2024-06-05 18:45:45 +00:00
|
|
|
end
|