Class: Contentful::Management::ContentType

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

Overview

Constant Summary collapse

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

Constants included from Resource::SystemProperties

Resource::SystemProperties::SYS_COERCIONS

Constants included from Resource

Resource::COERCIONS

Instance Attribute Summary

Attributes included from Resource::SystemProperties

#sys

Attributes included from Resource

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource::Refresher

#refresh_data, #reload

Methods included from Resource::SystemProperties

included, #initialize, #inspect

Methods included from Resource

#array?, #initialize, #inspect, #nested_locale_fields?, #sys

Class Method Details

.all(space_id, query = {}) ⇒ Object

Gets a collection of content types. Takes an id of space and an optional hash of query options Returns a Contentful::Management::Array of Contentful::Management::ContentType.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/contentful/management/content_type.rb', line 36

def self.all(space_id, query = {})
  request = Request.new(
      "/#{ space_id }/content_types",
      query
  )
  response = request.get
  result = ResourceBuilder.new(response, {}, {})
  content_types = result.run
  client.update_dynamic_entry_cache!(content_types)
  content_types
end

.create(space_id, attributes) ⇒ Object

Creates a content type. Takes an id of space and hash with attributes. Returns a Contentful::Management::ContentType.



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/contentful/management/content_type.rb', line 106

def self.create(space_id, attributes)
  fields = fields_to_nested_properties_hash(attributes[:fields] || [])
  request = Request.new(
      "/#{ space_id }/content_types/#{ attributes[:id]}",
      name: attributes.fetch(:name),
      description: attributes[:description],
      fields: fields
  )
  response = attributes[:id].nil? ? request.post : request.put
  result = ResourceBuilder.new(response, {}, {}).run
  client.register_dynamic_entry(result.id, DynamicEntry.create(result)) if result.is_a?(self.class)
  result
end

.find(space_id, content_type_id) ⇒ Object

Gets a specific entry. Takes an id of space and content type. Returns a Contentful::Management::ContentType.



51
52
53
54
55
56
57
58
# File 'lib/contentful/management/content_type.rb', line 51

def self.find(space_id, content_type_id)
  request = Request.new("/#{ space_id }/content_types/#{ content_type_id }")
  response = request.get
  result = ResourceBuilder.new(response, {}, {})
  content_type = result.run
  client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type)) if content_type.is_a?(self)
  content_type
end

Instance Method Details

#activateObject

Activates a content type. Returns a Contentful::Management::ContentType.



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

def activate
  request = Request.new(
      "/#{ space.id }/content_types/#{ id }/published",
      {},
      id = nil,
      version: sys[:version]
  )
  response = request.put
  result = ResourceBuilder.new(response, {}, {}).run
  refresh_data(result)
end

#active?Boolean

Checks if a content type is active. Returns true if active.

Returns:

  • (Boolean)


99
100
101
# File 'lib/contentful/management/content_type.rb', line 99

def active?
  sys[:publishedAt] ? true : false
end

#deactivateObject

Deactivates a content type. Only content type that has no entries can be deactivated. Returns a Contentful::Management::ContentType.



90
91
92
93
94
95
# File 'lib/contentful/management/content_type.rb', line 90

def deactivate
  request = Request.new("/#{ space.id }/content_types/#{ id }/published")
  response = request.delete
  result = ResourceBuilder.new(response, {}, {}).run
  refresh_data(result)
end

#destroyObject

Destroys a content type. Returns true if succeed.



62
63
64
65
66
67
68
69
70
71
# File 'lib/contentful/management/content_type.rb', line 62

def destroy
  request = Request.new("/#{ space.id }/content_types/#{ id }")
  response = request.delete
  if response.status == :no_content
    return true
  else
    result = ResourceBuilder.new(response, {}, {})
    result.run
  end
end

#entriesObject

Use this method only in the context of content type. Allows you to create an entry. Returns a Contentful::Management::Entry. See README for details.



202
203
204
# File 'lib/contentful/management/content_type.rb', line 202

def entries
  Contentful::Management::ContentTypeEntryMethodsFactory.new(self)
end

#fieldsObject

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 a Contentful::Management::ContentType. See README for details.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/contentful/management/content_type.rb', line 169

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

#merged_fields(new_field) ⇒ Object

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



152
153
154
155
156
157
158
159
160
161
# File 'lib/contentful/management/content_type.rb', line 152

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_fieldsObject



163
# File 'lib/contentful/management/content_type.rb', line 163

alias_method :orig_fields, :fields

#saveObject

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



142
143
144
145
146
147
148
149
# File 'lib/contentful/management/content_type.rb', line 142

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

#update(attributes) ⇒ Object

Updates a content type. Takes a hash with attributes. Returns a Contentful::Management::ContentType.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/contentful/management/content_type.rb', line 123

def update(attributes)
  parameters = {}
  parameters.merge!(displayField: attributes[:displayField]) if  attributes[:displayField]
  parameters.merge!(name: (attributes[:name] || name))
  parameters.merge!(description: (attributes[:description] || description))
  parameters.merge!(fields: self.class.fields_to_nested_properties_hash(attributes[:fields] || fields))
  request = Request.new(
      "/#{ space.id }/content_types/#{ id }",
      parameters,
      id = nil,
      version: sys[:version]
  )
  response = request.put
  result = ResourceBuilder.new(response, {}, {}).run
  refresh_data(result)
end