Class: Contentful::Management::ContentType
- Inherits:
-
Object
- Object
- Contentful::Management::ContentType
- Includes:
- Resource, Resource::Refresher, Resource::SystemProperties
- Defined in:
- lib/contentful/management/content_type.rb
Overview
Resource class for ContentType. www.contentful.com/developers/documentation/content-management-api/#resources-content-types
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
Instance Attribute Summary
Attributes included from Resource::SystemProperties
Attributes included from Resource
#client, #default_locale, #properties, #raw_object, #request
Class Method Summary collapse
-
.all(space_id, query = {}) ⇒ Object
Gets a collection of content types.
-
.create(space_id, attributes) ⇒ Object
Creates a content type.
-
.find(space_id, content_type_id) ⇒ Object
Gets a specific entry.
Instance Method Summary collapse
-
#activate ⇒ Object
Activates a content type.
-
#active? ⇒ Boolean
Checks if a content type is active.
-
#deactivate ⇒ Object
Deactivates a content type.
-
#destroy ⇒ Object
Destroys a content type.
-
#entries ⇒ Object
Use this method only in the context of content type.
-
#fields ⇒ Object
Use this method only in the context of content type.
-
#merged_fields(new_field) ⇒ Object
This method merges existing fields with new one, when adding, creating or updating new fields.
- #orig_fields ⇒ Object
-
#save ⇒ Object
If a content type is a new object gets created in the Contentful, otherwise the existing entry gets updated.
-
#update(attributes) ⇒ Object
Updates a content type.
Methods included from Resource::Refresher
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.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/contentful/management/content_type.rb', line 37 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.
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/contentful/management/content_type.rb', line 107 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.
52 53 54 55 56 57 58 59 |
# File 'lib/contentful/management/content_type.rb', line 52 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
#activate ⇒ Object
Activates a content type. Returns a Contentful::Management::ContentType.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/contentful/management/content_type.rb', line 76 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.
100 101 102 |
# File 'lib/contentful/management/content_type.rb', line 100 def active? sys[:publishedAt] ? true : false end |
#deactivate ⇒ Object
Deactivates a content type. Only content type that has no entries can be deactivated. Returns a Contentful::Management::ContentType.
91 92 93 94 95 96 |
# File 'lib/contentful/management/content_type.rb', line 91 def deactivate request = Request.new("/#{ space.id }/content_types/#{ id }/published") response = request.delete result = ResourceBuilder.new(response, {}, {}).run refresh_data(result) end |
#destroy ⇒ Object
Destroys a content type. Returns true if succeed.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/contentful/management/content_type.rb', line 63 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 |
#entries ⇒ Object
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.
203 204 205 |
# File 'lib/contentful/management/content_type.rb', line 203 def entries Contentful::Management::ContentTypeEntryMethodsFactory.new(self) end |
#fields ⇒ Object
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.
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 197 |
# File 'lib/contentful/management/content_type.rb', line 170 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.
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/contentful/management/content_type.rb', line 153 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 ⇒ Object
164 |
# File 'lib/contentful/management/content_type.rb', line 164 alias_method :orig_fields, :fields |
#save ⇒ Object
If a content type is a new object gets created in the Contentful, otherwise the existing entry gets updated. See README for details.
143 144 145 146 147 148 149 150 |
# File 'lib/contentful/management/content_type.rb', line 143 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.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/contentful/management/content_type.rb', line 124 def update(attributes) parameters = {} parameters.merge!(displayField: attributes[:displayField] || display_field) 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 |