Sha256: cc0846a2f113bf753464147793c09d82ca2b686e1412f8137cf2cff8769ace32

Contents?: true

Size: 1.85 KB

Versions: 10

Compression:

Stored size: 1.85 KB

Contents

module Scrivito
  class ObjClassController < WebserviceController
    def defaults
      obj_class_name = params.fetch(:obj_class_name)
      obj_class = fetch_obj_class(obj_class_name)
      if obj_class
        attributes = params.fetch(:attributes, {}).merge(_obj_class: obj_class.to_s) # <-- PATCH HERE
        defaults = obj_class.build_attributes_with_defaults(
          attributes, scrivito_user: scrivito_user)
        @defaults = computed_nested_attributes(defaults, obj_class)
      else
        raise ResourceNotFound, %{The obj class "#{obj_class_name}" is not available.}
      end
    end

    private

    def computed_nested_attributes(defaults, obj_class)
      widget_attributes = {}
      other_attributes = {}

      defaults.each do |name, value|
        next if value.nil?

        attribute_type = obj_class.attribute_definitions[name].try(:type)
        if attribute_type == 'widgetlist'
          widgets = value.map do |new_widget|
            computed_nested_attributes(new_widget.attributes_to_be_saved, new_widget.class)
          end

          widget_attributes[name] = ['widgetlist', widgets]
        elsif attribute_type != 'binary'
          other_attributes[name] = value
        end
      end

      other_attributes = serialize(other_attributes, obj_class)

      widget_attributes.merge(other_attributes).merge(_obj_class: obj_class.to_s) # <-- PATCH HERE
    end

    def serialize(attributes, obj_class)
      serializer = AttributeSerializer.new(obj_class)

      serializer.serialize(attributes, obj_class.attribute_definitions)
    end

    def fetch_obj_class(obj_class_name)
      compute_type(Widget, obj_class_name) || compute_type(Obj, obj_class_name)
    end

    def compute_type(base_class, obj_class_name)
      base_class.type_computer.compute_type_without_fallback(obj_class_name)
    rescue ObjClassNotFound
      nil
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
infopark_fiona7-1.5.5.5.1 app/controllers/scrivito/obj_class_controller.rb
infopark_fiona7-1.5.5.5.0 app/controllers/scrivito/obj_class_controller.rb
infopark_fiona7-1.5.5.4.1 app/controllers/scrivito/obj_class_controller.rb
infopark_fiona7-1.5.5.4.0 app/controllers/scrivito/obj_class_controller.rb
infopark_fiona7-1.5.5.3.1 app/controllers/scrivito/obj_class_controller.rb
infopark_fiona7-1.5.4.3.0 app/controllers/scrivito/obj_class_controller.rb
infopark_fiona7-1.5.3.2.0 app/controllers/scrivito/obj_class_controller.rb
infopark_fiona7-1.5.3.2.0.pre.beta1 app/controllers/scrivito/obj_class_controller.rb
infopark_fiona7-1.5.2.1.0.pre.beta1 app/controllers/scrivito/obj_class_controller.rb
infopark_fiona7-1.5.2.0.0 app/controllers/scrivito/obj_class_controller.rb