Sha256: 41b8eda091e429aa49d5a1fd0b17df8706226dbe79ec824035a9def7718c00a8

Contents?: true

Size: 1.71 KB

Versions: 10

Compression:

Stored size: 1.71 KB

Contents

module Scrivito

class WidgetFieldParams
  class UnknownWidgetAction < ScrivitoError
  end

  attr_reader :pool_params

  def initialize
    @pool_params = {}
  end

  def convert(params)
    widget_ids = []

    params.each do |widget_id_or_spec|
      if widget_id_or_spec.is_a?(Hash)
        action, spec = parse_action_and_spec(widget_id_or_spec)
        case action
        when 'create' then widget_ids << create_widget(spec)
        when 'copy' then widget_ids << copy_widget(*spec.values_at(:src_obj_id, :src_widget_id))
        else raise UnknownWidgetAction
        end
      else
        widget_ids << widget_id_or_spec
      end
    end

    {'list' => widget_ids.map { |widget_id| {'widget' => widget_id} }}
  end

  private

  def parse_action_and_spec(params)
    action, spec = params.flatten
    [action.to_s, spec.with_indifferent_access]
  end

  def create_widget(params)
    widget_pool_id = BasicObj.generate_widget_pool_id
    # At the moment the 'create' action supports only the '_obj_class' field.
    @pool_params[widget_pool_id] = {'_obj_class' => params['_obj_class']}
    widget_pool_id
  end

  def copy_widget(src_obj_id, src_widget_id)
    src_obj_content = CmsRestApi.get("workspaces/#{Workspace.current.id}/objs/#{src_obj_id}")
    widget_params = src_obj_content['_widget_pool']["#{src_widget_id}"]

    raise ResourceNotFound, "could not find widget with id #{src_widget_id}" unless widget_params

    src_widget = BasicObj.find(src_obj_id).widget_from_pool(src_widget_id)
    widget_params.delete_if { |field_name, _| src_widget.type_of_attribute(field_name) == 'widget' }

    widget_pool_id = BasicObj.generate_widget_pool_id
    @pool_params[widget_pool_id] = widget_params
    widget_pool_id
  end
end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
scrivito_sdk-0.30.0 lib/scrivito/widget_field_params.rb
scrivito_sdk-0.30.0.rc1 lib/scrivito/widget_field_params.rb
scrivito_sdk-0.18.1 lib/scrivito/widget_field_params.rb
scrivito_sdk-0.18.0 lib/scrivito/widget_field_params.rb
scrivito_sdk-0.17.0 lib/scrivito/widget_field_params.rb
scrivito_sdk-0.16.0 lib/scrivito/widget_field_params.rb
scrivito_sdk-0.15.0 lib/scrivito/widget_field_params.rb
scrivito_sdk-0.14.0 lib/scrivito/widget_field_params.rb
scrivito_sdk-0.13.0 lib/scrivito/widget_field_params.rb
scrivito_sdk-0.12.0 lib/scrivito/widget_field_params.rb