Sha256: 1153b316db08497e55dae25113726d5f83300460a95ba11b4b0a6b9b3637e359

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

module Locomotive
  module Wagon

    class ContentTypeDecorator < SimpleDelegator

      include ToHashConcern

      def initialize(entity, existing_fields = [])
        @existing_fields = existing_fields
        super(entity)
      end

      def __attributes__
        %i(name slug description label_field_name fields
          order_by order_direction group_by
          public_submission_enabled public_submission_accounts
          raw_item_template)
      end

      def fields
        return @fields if @fields

        @fields = __getobj__.fields.no_associations.map { |f| ContentTypeFieldDecorator.new(f) }

        @existing_fields.each do |name|
          # the field exists remotely but does not exist locally, delete it
          if __getobj__.fields.by_name(name).nil?
            @fields << { name: name, _destroy: true }
          end
        end

        @fields
      end

      def description
        self[:description]
      end

      def order_by
        self[:order_by]
      end

      def group_by
        self[:group_by]
      end

      def public_submission_enabled
        self[:public_submission_enabled]
      end

      def public_submission_accounts
        self[:public_submission_accounts]
      end

      def raw_item_template
        self[:raw_item_template]
      end

      def with_relationships?
        __getobj__.fields.associations.count > 0
      end

    end

    class ContentTypeWithOnlyRelationshipsDecorator < ContentTypeDecorator

      def __attributes__
        %i(name slug fields)
      end

      def fields
        @fields ||= __getobj__.fields.associations.map { |f| ContentTypeFieldDecorator.new(f) }
      end

    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
locomotivecms_wagon-2.0.0.pre.alpha.3 lib/locomotive/wagon/decorators/content_type_decorator.rb
locomotivecms_wagon-2.0.0.pre.alpha.2 lib/locomotive/wagon/decorators/content_type_decorator.rb
locomotivecms_wagon-2.0.0.pre.alpha.1 lib/locomotive/wagon/decorators/content_type_decorator.rb
locomotivecms_wagon-2.0.0.pre.alpha lib/locomotive/wagon/decorators/content_type_decorator.rb