Add template builder service

This commit is contained in:
Trevor Vallender 2024-06-30 08:46:38 +01:00
parent 032c18d05d
commit c870e373bc
1 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,40 @@
# frozen_string_literaL: true
class TemplateBuilder
class << self
def create!(name:, from:, game_system:)
Template.create!(
name:,
klass: from.class.name,
game_system:,
content: serialize_character_sheet_section(from),
)
end
private
def serialize_character_sheet_section(section)
always_except = [ :id, :created_at, :updated_at ]
subsection_except = [ :character_id, :parent_section_id ]
always_include = {
stats: { except: always_except + [ :value ] },
text_fields: { except: always_except },
}
section.to_json(
except: always_except + subsection_except,
include: {
character_sheet_subsections: {
except: always_except + subsection_except,
include: always_include.merge(
character_sheet_subsections: {
except: always_except + subsection_except,
include: always_include,
},
),
},
}.merge(always_include),
)
end
end
end