Add deserialize method to CharacterSheetSection

This commit is contained in:
Trevor Vallender 2024-06-30 09:14:55 +01:00
parent c870e373bc
commit d4b982ea16
2 changed files with 36 additions and 0 deletions

View File

@ -25,4 +25,22 @@ class CharacterSheetSection < ApplicationRecord
def highest_order_index
character_sheet_features.maximum(:order_index) || 1
end
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
end

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
require "test_helper"
class TemplateBuilderTest < ActiveSupport::TestCase
test "creates template from a CharacterSheetSection" do
section = character_sheet_sections(:stats)
template = TemplateBuilder.create!(
name: "Test template",
game_system: GameSystem.last,
from: section,
)
template_hash = JSON.parse(template.content)
assert_equal 1, template_hash["character_sheet_subsections"].count
assert_equal 3, template_hash["stats"].count
assert_equal "Stats", template_hash["name"]
end
end