41 lines
1.1 KiB
Ruby
41 lines
1.1 KiB
Ruby
|
# 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
|