class Contentful::Management::ContentType

Resource class for ContentType. @see _ www.contentful.com/developers/documentation/content-management-api/#resources-content-types

Constants

FIELD_TYPES

Shortcuts for Contentful Field Types

Public Class Methods

client_association_class() click to toggle source

@private

# File lib/contentful/management/content_type.rb, line 45
def self.client_association_class
  ClientContentTypeMethodsFactory
end
create_attributes(_client, attributes) click to toggle source

@private

# File lib/contentful/management/content_type.rb, line 54
def self.create_attributes(_client, attributes)
  fields = fields_to_nested_properties_hash(attributes[:fields] || [])

  params = attributes.clone
  params[:fields] = fields
  params.delete(:id)
  params.delete_if { |_, v| v.nil? }
end
fields_to_nested_properties_hash(fields) click to toggle source

@private

# File lib/contentful/management/content_type.rb, line 157
def self.fields_to_nested_properties_hash(fields)
  fields.map do |field|
    field.properties.replace(field.properties_to_hash)
  end
end

Public Instance Methods

after_create(_attributes) click to toggle source

@private

# File lib/contentful/management/content_type.rb, line 64
def after_create(_attributes)
  client.register_dynamic_entry(id, DynamicEntry.create(self, client))
end
display_field_value(attributes) click to toggle source

@private

# File lib/contentful/management/content_type.rb, line 69
def display_field_value(attributes)
  if attributes[:displayField].nil? && (display_field.nil? || display_field.empty?)
    nil
  else
    attributes[:displayField] || display_field
  end
end
editor_interface() click to toggle source

Use this method only in the context of content type. Allows you to create an editor interface. @see _ README for details.

@private

@return [Contentful::Management::ContentTypeEditorInterfaceMethodsFactory]

# File lib/contentful/management/content_type.rb, line 143
def editor_interface
  Contentful::Management::ContentTypeEditorInterfaceMethodsFactory.new(self)
end
entries() click to toggle source

Use this method only in the context of content type. Allows you to create an entry. @see _ README for details.

@private

@return [Contentful::Management::ContentTypeEntryMethodsFactory]

# File lib/contentful/management/content_type.rb, line 132
def entries
  Contentful::Management::ContentTypeEntryMethodsFactory.new(self)
end
fields() click to toggle source

Use this method only in the context of content type. Allows you to add and create a field with specified attributes or destroy by pass field id. @see _ README for details.

@return [Contentful::Management::ContentType]

# File lib/contentful/management/content_type.rb, line 98
def fields
  fields = orig_fields

  fields.instance_exec(self) do |content_type|
    fields.define_singleton_method(:add) do |field|
      content_type.update(fields: content_type.merged_fields(field))
    end

    fields.define_singleton_method(:create) do |params|
      field = Contentful::Management::Field.new
      Field.property_coercions.each do |key, _value|
        snakify_key = Support.snakify(key)
        param = params[snakify_key.to_sym]
        field.send("#{snakify_key}=", param) if param
      end
      content_type.update(fields: content_type.merged_fields(field))
    end

    fields.define_singleton_method(:destroy) do |id|
      fields = content_type.fields.reject { |field| field.id == id }
      content_type.update(fields: fields)
    end
  end

  fields
end
Also aliased as: orig_fields
merged_fields(new_field) click to toggle source

This method merges existing fields with new one, when adding, creating or updating new fields. @private

# File lib/contentful/management/content_type.rb, line 79
def merged_fields(new_field)
  field_ids = []
  merged_fields = fields.each_with_object([]) do |field, fields|
    field.deep_merge!(new_field) if field.id == new_field.id
    fields << field
    field_ids << field.id
  end
  merged_fields << new_field unless field_ids.include?(new_field.id)
  merged_fields
end
orig_fields()

@private

Alias for: fields
snapshots() click to toggle source

Allows manipulation of snapshots in context of the current content type Allows listing all snapshots belonging to this entry and finding one by id. @see _ README for details.

@return [Contentful::Management::ContentTypeSnapshotMethodsFactory]

# File lib/contentful/management/content_type.rb, line 152
def snapshots
  ContentTypeSnapshotMethodsFactory.new(self)
end

Protected Instance Methods

query_attributes(attributes) click to toggle source
# File lib/contentful/management/content_type.rb, line 165
def query_attributes(attributes)
  parameters = {}
  parameters[:displayField] = display_field_value(attributes)
  parameters[:name] = attributes[:name] || name
  parameters[:description] = attributes[:description] || description
  parameters[:fields] = self.class.fields_to_nested_properties_hash(attributes[:fields] || fields)

  parameters.delete_if { |_, v| v.nil? }
end