Sha256: 745d34aa2df1664d97da6245712297f4e1dbe21bbcfa68d0efdaff08eeb4bcdd

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

module Scrivito
  module ObjParamsParser
    class UnknownWidgetAction < ScrivitoError
    end

    def parse(params)
      params = params.deep_dup
      convert_params(params)
      params
    end

    private

    def convert_field_params(params, attribute_definitions)
      params.each_pair do |key, value|
        params[key] = case type = attribute_definitions[key.to_s].try(:type)
          when 'binary'     then parse_binary_params(value)
          when 'html'       then ContentConversion.convert_html_links(value, host, port)
          when 'link'       then ContentConversion.convert_link(value, host, port)
          when 'linklist'   then ContentConversion.convert_linklist_urls(value, host, port)
          when 'widgetlist' then parse_widgetlist_params(value)
          when 'date'       then parse_date_params(value)
          else value
          end
      end
    end

    def parse_widgetlist_params(params)
      params.map do |widget_id_or_params|
        if widget_id_or_params.is_a?(Hash)
          action, widget_params = widget_id_or_params.flatten
          case action
          when 'create' then Widget.new(widget_params)
          when 'copy'
            widget_id = widget_params['widget_id']
            widget = Workspace.find(widget_params['workspace_id'])
              .objs.find(widget_params['obj_id'])
              .widgets[widget_id]
            raise ResourceNotFound, "Could not find Widget with id #{widget_id}" unless widget
            widget.copy
          else raise UnknownWidgetAction
          end
        else
          obj.widgets[widget_id_or_params]
        end
      end
    end

    def parse_binary_params(params)
      UploadedBinary.new(params) if params
    end

    def parse_date_params(params)
      DateAttribute.parse(params)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scrivito_sdk-0.60.0 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.60.0.rc2 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.60.0.rc1 lib/scrivito/obj_params_parser.rb