Add template builder service
This commit is contained in:
parent
032c18d05d
commit
c870e373bc
|
@ -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
|
Loading…
Reference in New Issue