Sha256: c3ed1cf33bf8c1b26b83059061c711c7c0d9c1331ff398198663896d0a19da9f

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

module Scrivito
  class CmsRestApi
    class WidgetExtractor

      # Extract widgets from a given attribute tree and return them as a flat
      # hash of widget_like => updated_attributes
      def self.call(updated_attributes, obj=nil)
        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))
        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))
              end
            end
          end
        end

        extracted_widgets
      end

      def self.attach_widget_to_obj(widget, obj)
        widget.id = if obj.present?
          obj.generate_widget_pool_id
        else
          BasicObj.generate_widget_pool_id
        end
        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_attribtues
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
scrivito_sdk-0.30.0 lib/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-0.30.0.rc1 lib/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-0.18.1 lib/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-0.18.0 lib/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-0.17.0 lib/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-0.16.0 lib/scrivito/cms_rest_api/widget_extractor.rb