module Scrivito module ClientAttributeSerializer def self.serialize_obj(obj) serialized_output = {} # system attributes serialized_output.merge!(serialize_system_attrs(obj)) # custom attributes serialized_output.merge!(serialize_custom_attrs(obj)) # _widget_pool serialized_output.merge!(serialize_widget_pool(obj.widgets)) serialized_output end def self.serialize_system_attrs(obj) { '_id' => obj.id, '_obj_class' => obj.obj_class_name, '_path' => obj.path, '_permalink' => obj.permalink, } end def self.serialize_custom_attrs(obj_or_widget) Hash[ obj_or_widget.attribute_definitions.map do |definition| name = definition.name type = definition.type value = obj_or_widget[name] [name, serialize_custom_attr_value(type, value)] end ] end def self.serialize_widget_pool(widgets) { '_widget_pool' => Hash[ widgets.map do |widget| [widget.id, serialize_widget(widget)] end ] } end def self.serialize_custom_attr_value(type, value) return unless value case type when 'binary' then serialize_binary_value(value) when 'date' then serialize_date_value(value) when 'enum' then serialize_enum_value(value) when 'html' then serialize_html_value(value) when 'link' then serialize_link_value(value) when 'linklist' then serialize_linklist_value(value) when 'multienum' then serialize_multienum_value(value) when 'reference' then serialize_reference_value(value) when 'referencelist' then serialize_referencelist_value(value) when 'string' then serialize_string_value(value) when 'stringlist' then serialize_stringlist_value(value) when 'widgetlist' then serialize_widgetlist_value(value) end end def self.serialize_binary_value(value) {'url' => value.url, 'filename' => value.filename} end def self.serialize_date_value(value) DateAttribute.serialize_for_client(value) end def self.serialize_enum_value(value) value end def self.serialize_html_value(value) value end def self.serialize_link_value(value) value.to_cms_api_linklist_params.stringify_keys end def self.serialize_linklist_value(value) value.map do |link| serialize_link_value(link) end end def self.serialize_multienum_value(value) value.map do |enum| serialize_enum_value(enum) end end def self.serialize_reference_value(value) value.id end def self.serialize_referencelist_value(value) value.map do |reference| serialize_reference_value(reference) end end def self.serialize_string_value(value) value end def self.serialize_stringlist_value(value) value.map do |string| serialize_string_value(string) end end def self.serialize_widgetlist_value(value) value.map do |widget| widget.id end end def self.serialize_widget(widget) { '_obj_class' => widget.obj_class_name }.merge(serialize_custom_attrs(widget)) end end end