module Scrivito class CmsRestApi module AttributeSerializer class << self def generate_widget_pool_changes(widget_hash) widget_pool_changes = {} widget_hash.each do |widget, attributes| if attributes.present? widget_pool_changes[widget.id] = self.convert(attributes) else # Empty hash or nil widget_pool_changes[widget.id] = attributes end end widget_pool_changes end def convert(attributes) Hash[ attributes.map do |attribute_name, value| attribute_name = attribute_name.to_s converted_value = if value.nil? || value == [] nil elsif link_array?(value) convert_links(value) elsif value.is_a?(Link) convert_link(value) elsif value.is_a?(BasicObj) value.id elsif widget_array?(value) convert_widgets(value) elsif obj_array?(value) value.map(&:id) elsif value.is_a?(Time) || value.is_a?(Date) convert_time(value) elsif value.is_a?(Array) value.map(&:to_s) elsif value.is_a?(File) Scrivito::CmsRestApi::BlobUploader.upload_file(value) else value.to_s end [attribute_name, converted_value] end ] end def convert_time(point_in_time) time_object = if point_in_time.instance_of?(Date) point_in_time.to_time else point_in_time.to_time.utc end time_object.strftime("%Y%m%d%H%M%S") end private def widget_array?(value) value.is_a?(Array) && value.all? do |element| element.is_a?(BasicWidget) end end def convert_widgets(value) converted_widgets = value.map do |widget| { 'widget' => widget.id } end { 'list' => converted_widgets } end def link_array?(value) value.is_a?(Array) && value.all? do |element| element.is_a?(Link) end end def obj_array?(value) value.is_a?(Array) && value.all? do |element| element.is_a?(BasicObj) end end def convert_links(links) links.map do |link| convert_link(link) end end def convert_link(link) link.to_cms_api_linklist_params end end end end end