Sha256: 93a44acb00501b22217cdc90c2aca397fdb2d79ce1260463467e6bf8b1b55999
Contents?: true
Size: 1.83 KB
Versions: 85
Compression:
Stored size: 1.83 KB
Contents
module Scrivito class CmsRestApi class WidgetExtractor # Extract widgets from a given attribute tree # first return param: a flat hash of widget_like => updated widget attributes # second return param: the updated attributes def self.call(attributes, obj=nil) updated_attributes = attributes.dup extracted_widgets = {} (updated_attributes.delete(:_widget_pool) || {}).each do |widget, widget_properties| unless widget.persisted? raise ScrivitoError, 'new top-level widgets are not allowed in the _widget_pool' end extracted_widgets[widget] = widget_properties extracted_widgets.merge!(call(widget_properties, obj).first) end updated_attributes.each do |attribute_name, value| if value.is_a?(Array) && value.all? {|w| w.is_a?(BasicWidget)} value.each do |widget| unless widget.persisted? attach_widget_to_obj(widget, obj) extracted_widgets[widget] = widget.attributes_to_be_saved extracted_widgets.merge!(call(widget.attributes_to_be_saved, obj).first) end end end end return extracted_widgets, updated_attributes end def self.attach_widget_to_obj(widget, obj) widget.id = widget.attributes_to_be_saved.delete('_id') || generate_widget_pool_id(obj) widget.obj = obj end def self.notify_persisted_widgets(obj, widget_hash) widget_hash.each do |widget, attributes| if attributes.present? widget.obj = obj widget.forget_unsaved_attributes end end end def self.generate_widget_pool_id(obj) obj.presence ? obj.generate_widget_pool_id : BasicObj.generate_widget_pool_id end end end end
Version data entries
85 entries across 85 versions & 1 rubygems