Class: Contentful::Management::ContentType

Inherits:
Object
  • Object
show all
Extended by:
Resource::AllPublished
Includes:
Resource, Resource::Publisher, Resource::Refresher, Resource::SystemProperties
Defined in:
lib/contentful/management/content_type.rb

Overview

Resource class for ContentType.

See Also:

  • https://www.contentful.com/developers/documentation/content-management-api/#resources-content-types

Constant Summary collapse

FIELD_TYPES =

Shortcuts for Contentful Field Types

[
  SYMBOL = 'Symbol',
  TEXT = 'Text',
  INTEGER = 'Integer',
  FLOAT = 'Number',
  DATE = 'Date',
  BOOLEAN = 'Boolean',
  LINK = 'Link',
  ARRAY = 'Array',
  OBJECT = 'Object',
  LOCATION = 'Location'
]

Instance Attribute Summary

Attributes included from Resource::SystemProperties

#sys

Attributes included from Resource

#client, #default_locale, #properties, #raw_object, #request

Instance Method Summary collapse

Methods included from Resource::AllPublished

all_published

Methods included from Resource::Publisher

#publish, #published?, #unpublish

Methods included from Resource::Refresher

#reload

Methods included from Resource

#array?, #destroy, #nested_locale_fields?, #resource?, #sys, #update

Instance Method Details

#fieldsContentful::Management::ContentType

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.

Returns:

See Also:

  • README for details.


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/contentful/management/content_type.rb', line 107

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.select { |field| field.id != id }
      content_type.update(fields: fields)
    end
  end

  fields
end

#saveContentful::Management::ContentType

If a content type is a new object gets created in the Contentful, otherwise the existing entry gets updated.

Returns:

See Also:

  • README for details.


77
78
79
80
81
82
83
84
# File 'lib/contentful/management/content_type.rb', line 77

def save
  if id
    update(@properties)
  else
    new_instance = self.class.create(client, space.id, @properties)
    refresh_data(new_instance)
  end
end