Sha256: c43e30c1672c31a7a14d7d14f9f07d343e2c5fd222f30d093481772de0344629

Contents?: true

Size: 1.42 KB

Versions: 16

Compression:

Stored size: 1.42 KB

Contents

module Scrivito
  class UiObjClassesJsonFormatter < Struct.new(:lookup_context)
    def format(obj_class)
      data = base_obj_class_data(obj_class)
      if obj_class.ancestors.include?(Scrivito::BasicWidget)
        data[:embeds] = obj_class.embeds?
        data[:embeddingAttribute] =
          obj_class.embeds? ? obj_class.embedding_attribute.name.camelcase(:lower) : nil

        data[:validContainerClasses] = convert_classes_array(obj_class.valid_container_classes)
      end

      data[:hasServerDetailsTemplate] = details_view?(obj_class.name)
      data
    end

    private

    def base_obj_class_data(obj_class)
      {
        name: obj_class.name,
        type: obj_class.ancestors.include?(Scrivito::BasicWidget) ? 'Widget' : 'Obj',
        title: obj_class.description_for_editor,
        attributes: obj_class.attribute_definitions.map { |attr| convert_attribute(attr) },
        usesServerCallbacks: true,
        hideInSelectionDialogs: obj_class.hide_from_editor?,
      }
    end

    def convert_attribute(attribute)
      attribute_json = attribute.as_json
      attribute_json[:name] = attribute_json[:name].camelcase(:lower)
      attribute_json
    end

    def details_view?(obj_class_name)
      lookup_context.find("#{obj_class_name.underscore}/details").present?
    rescue ActionView::MissingTemplate
      false
    end

    def convert_classes_array(arr)
      if arr
        arr.map(&:name)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
scrivito_sdk-1.18.0 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.18.0.rc1 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.17.0 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.17.0.rc3 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.17.0.rc2 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.17.0.rc1 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.16.0 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.16.0.rc2 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.16.0.rc1 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.15.0 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.15.0.rc2 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.15.0.rc1 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.14.0 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.14.0.rc3 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.14.0.rc2 app/cms/scrivito/ui_obj_classes_json_formatter.rb
scrivito_sdk-1.14.0.rc1 app/cms/scrivito/ui_obj_classes_json_formatter.rb