Sha256: 79bcc3e4ddaf9551d35acb4d12d85ee3764d723edcffb5ab338f2c702787f761

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 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(obj_class, params)
      params.each_pair do |key, value|
        if key.to_s == 'blob' && obj_class.legacy_type?
          params[key] = parse_binary_field_params(value)
          next
        end

        params[key] = case type = obj_class.attributes[key].try(:type)
          when 'html'     then ContentConversion.convert_html_links(value, host, port)
          when 'linklist' then ContentConversion.convert_linklist_urls(value, host, port)
          when 'link'     then ContentConversion.convert_link(value, host, port)
          when 'widget'   then parse_widget_field_params(value)
          when 'binary'   then parse_binary_field_params(value)
          else value
          end
      end
    end

    def parse_widget_field_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['src_widget_id']
            widget = Obj.find(widget_params['src_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_field_params(params)
      UploadedBinary.new(params) if params
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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