diff --git a/app/models/communication/block/template/base/with_data.rb b/app/models/communication/block/template/base/with_data.rb index 915626bdba8db90d6e6c2eaf08f5e875ffe738ab..05de95014dee2d702852d3066e64ea509c728465 100644 --- a/app/models/communication/block/template/base/with_data.rb +++ b/app/models/communication/block/template/base/with_data.rb @@ -8,15 +8,7 @@ module Communication::Block::Template::Base::WithData next unless json.has_key? component.property component.data = json[component.property] end - return unless has_element_class? # Template is not supposed to have elements at all - return unless json.has_key?('elements') # Template has no element yet - # Objects are initialized from the database, - # then data from the form replaces data from the db. - # We need to reset elements, otherwise it's never deleted. - @elements = [] - json['elements'].each do |json| - @elements << default_element(json) - end + initialize_elements json end # Reads the data from the components @@ -64,6 +56,18 @@ module Communication::Block::Template::Base::WithData !self.class.element_class.nil? end + def initialize_elements(json) + return unless has_element_class? # Template is not supposed to have elements at all + return unless json.has_key?('elements') # Template has no element yet + # Objects are initialized from the database, + # then data from the form replaces data from the db. + # We need to reset elements, otherwise it's never deleted. + @elements = [] + json['elements'].each do |json| + @elements << default_element(json) + end + end + def json_from(value) if value.is_a? String JSON.parse(value)