Sha256: d9a9f189a770c3f72fee5bbd14084043e45c70f9e659ac0d91bcac9e1dff56ca

Contents?: true

Size: 1.83 KB

Versions: 16

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

16 entries across 16 versions & 1 rubygems

Version Path
scrivito_sdk-1.16.0 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.16.0.rc2 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.16.0.rc1 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.15.0 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.15.0.rc2 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.15.0.rc1 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.14.0 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.14.0.rc3 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.14.0.rc2 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.14.0.rc1 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.13.0 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.13.0.rc2 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.13.0.rc1 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.12.0 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.12.0.rc2 app/cms/scrivito/cms_rest_api/widget_extractor.rb
scrivito_sdk-1.12.0.rc1 app/cms/scrivito/cms_rest_api/widget_extractor.rb