contentful-management 0.0.1.pre → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +8 -0
- data/LICENSE.txt +17 -18
- data/README.md +10 -19
- data/examples/blog.rb +2 -0
- data/examples/create_space.rb +1 -1
- data/lib/contentful/management.rb +0 -8
- data/lib/contentful/management/asset.rb +41 -38
- data/lib/contentful/management/client.rb +8 -1
- data/lib/contentful/management/content_type.rb +44 -6
- data/lib/contentful/management/entry.rb +61 -29
- data/lib/contentful/management/field.rb +1 -1
- data/lib/contentful/management/file.rb +1 -0
- data/lib/contentful/management/link.rb +1 -1
- data/lib/contentful/management/locale.rb +24 -8
- data/lib/contentful/management/location.rb +1 -1
- data/lib/contentful/management/resource/asset_fields.rb +17 -0
- data/lib/contentful/management/resource/entry_fields.rb +13 -0
- data/lib/contentful/management/resource/fields.rb +1 -0
- data/lib/contentful/management/resource_builder.rb +0 -1
- data/lib/contentful/management/space.rb +38 -10
- data/lib/contentful/management/support.rb +3 -1
- data/lib/contentful/management/version.rb +1 -1
- data/spec/fixtures/vcr_cassettes/asset/publish_after_create.yml +268 -0
- data/spec/fixtures/vcr_cassettes/content_type/entry/all.yml +777 -0
- data/spec/fixtures/vcr_cassettes/entry/content_type_entires.yml +156 -0
- data/spec/fixtures/vcr_cassettes/entry/create_with_custom_id.yml +287 -0
- data/spec/fixtures/vcr_cassettes/space/{asset/assets.yml → entry/content_type_entires.yml} +369 -367
- data/spec/lib/contentful/management/asset_spec.rb +32 -23
- data/spec/lib/contentful/management/content_type_spec.rb +31 -6
- data/spec/lib/contentful/management/entry_spec.rb +21 -3
- data/spec/lib/contentful/management/space_spec.rb +26 -37
- metadata +22 -17
- data/spec/fixtures/vcr_cassettes/asset/image_url.yml +0 -133
- data/spec/fixtures/vcr_cassettes/space/content_type/content_types.yml +0 -341
- data/spec/fixtures/vcr_cassettes/space/entry/entries.yml +0 -498
- data/spec/fixtures/vcr_cassettes/space/locale/locales.yml +0 -480
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
@@ -1,22 +1,21 @@
|
|
1
|
-
|
1
|
+
The MIT License (MIT)
|
2
2
|
|
3
|
-
|
3
|
+
Copyright (c) 2014 Contentful GmbH
|
4
4
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
the following conditions:
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
12
11
|
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
15
14
|
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
OF
|
22
|
-
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
+
|
1
2
|
# Contentful::Management
|
3
|
+
[](http://badge.fury.io/rb/contentful-management) [](https://travis-ci.org/contentful/contentful-management.rb)
|
2
4
|
|
3
5
|
Ruby client for the Contentful Content Management API.
|
4
6
|
|
@@ -79,12 +81,6 @@ blog_space.save
|
|
79
81
|
|
80
82
|
Retrieving all content types from a space:
|
81
83
|
|
82
|
-
```ruby
|
83
|
-
blog_post_content_types = blog_space.content_types
|
84
|
-
```
|
85
|
-
|
86
|
-
or
|
87
|
-
|
88
84
|
```ruby
|
89
85
|
blog_post_content_types = blog_space.content_types.all
|
90
86
|
```
|
@@ -170,12 +166,6 @@ blog_post_content_type.update(name: 'Post', description: 'Post Description', fie
|
|
170
166
|
|
171
167
|
Retrieving all locales from the space:
|
172
168
|
|
173
|
-
```ruby
|
174
|
-
blog_post_locales = blog_space.locales
|
175
|
-
```
|
176
|
-
|
177
|
-
or
|
178
|
-
|
179
169
|
```ruby
|
180
170
|
blog_post_locales = blog_space.locales.all
|
181
171
|
```
|
@@ -200,11 +190,6 @@ blog_post_locale.update(name: 'German', code: 'de-DE')
|
|
200
190
|
|
201
191
|
Retrieving all assets from the space:
|
202
192
|
|
203
|
-
```ruby
|
204
|
-
blog_post_assets = blog_space.assets
|
205
|
-
```
|
206
|
-
|
207
|
-
or
|
208
193
|
|
209
194
|
```ruby
|
210
195
|
blog_post_assets = blog_space.assets.all
|
@@ -308,13 +293,19 @@ my_image_asset.published?
|
|
308
293
|
Retrieving all entries from the space:
|
309
294
|
|
310
295
|
```ruby
|
311
|
-
entries = blog_space.entries
|
296
|
+
entries = blog_space.entries.all
|
297
|
+
```
|
298
|
+
|
299
|
+
Retrieving all entries from the space with given content type:
|
300
|
+
|
301
|
+
```ruby
|
302
|
+
entries = blog_space.entries.all(content_type_id: content_type.id)
|
312
303
|
```
|
313
304
|
|
314
305
|
or
|
315
306
|
|
316
307
|
```ruby
|
317
|
-
entries =
|
308
|
+
entries = content_type.entries.all
|
318
309
|
```
|
319
310
|
|
320
311
|
Retrieving an entry by id:
|
data/examples/blog.rb
CHANGED
@@ -27,11 +27,13 @@ blog_space.locales.create(name: 'German', code: 'de-DE')
|
|
27
27
|
category_type = blog_space.content_types.create(name: 'Category')
|
28
28
|
category_type.fields.create(id: 'category_name', name: 'Category Name', type: 'Text', localized: true)
|
29
29
|
category_type.fields.create(id: 'category_description', name: 'Category Description', type: 'Text', localized: true)
|
30
|
+
category_type.update(displayField: 'category_name')
|
30
31
|
|
31
32
|
post_type = blog_space.content_types.create(name: 'Post')
|
32
33
|
post_type.fields.create(id: 'post_title', name: 'Post Title', type: 'Text', localized: true)
|
33
34
|
post_type.fields.create(id: 'post_author', name: 'Post Author', type: 'Text', localized: true)
|
34
35
|
post_type.fields.create(id: 'post_body', name: 'Post Body', type: 'Text', localized: true)
|
36
|
+
post_type.update(displayField: 'post_title')
|
35
37
|
|
36
38
|
categories = Contentful::Management::Field.new
|
37
39
|
categories.id = 'post_category'
|
data/examples/create_space.rb
CHANGED
@@ -18,7 +18,7 @@ dat_space = Contentful::Management::Space.find(my_space.id)
|
|
18
18
|
|
19
19
|
dat_space.locales.create(name: 'English', code: 'en-US')
|
20
20
|
|
21
|
-
locales = dat_space.locales
|
21
|
+
locales = dat_space.locales.all
|
22
22
|
# locales.map(&:destroy) # not implemented yet?
|
23
23
|
|
24
24
|
dat_space.destroy
|
@@ -1,12 +1,4 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require 'contentful/management/version'
|
3
3
|
require 'contentful/management/support'
|
4
|
-
require 'contentful/management/resource'
|
5
4
|
require 'contentful/management/client'
|
6
|
-
require 'contentful/management/space'
|
7
|
-
|
8
|
-
module Contentful
|
9
|
-
module Management
|
10
|
-
# Your code goes here...
|
11
|
-
end
|
12
|
-
end
|
@@ -1,26 +1,23 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require_relative 'resource'
|
3
|
+
require_relative 'resource/asset_fields'
|
3
4
|
require_relative 'resource/fields'
|
4
5
|
|
5
6
|
module Contentful
|
6
7
|
module Management
|
8
|
+
# Resource class for Asset.
|
9
|
+
# https://www.contentful.com/developers/documentation/content-management-api/#resources-assets
|
7
10
|
class Asset
|
8
11
|
|
9
|
-
FIELDS_COERCIONS = {
|
10
|
-
title: :hash,
|
11
|
-
description: :hash,
|
12
|
-
file: Contentful::Management::File
|
13
|
-
}
|
14
|
-
|
15
|
-
def self.fields_coercions
|
16
|
-
FIELDS_COERCIONS
|
17
|
-
end
|
18
|
-
|
19
12
|
include Contentful::Management::Resource
|
13
|
+
extend Contentful::Management::Resource::AssetFields
|
20
14
|
include Contentful::Management::Resource::Fields
|
21
15
|
include Contentful::Management::Resource::SystemProperties
|
22
16
|
include Contentful::Management::Resource::Refresher
|
23
17
|
|
18
|
+
# Gets a collection of assets.
|
19
|
+
# Takes an id of space.
|
20
|
+
# Returns a Contentful::Management::Array of Contentful::Management::Asset.
|
24
21
|
def self.all(space_id)
|
25
22
|
request = Request.new("/#{ space_id }/assets")
|
26
23
|
response = request.get
|
@@ -28,6 +25,9 @@ module Contentful
|
|
28
25
|
result.run
|
29
26
|
end
|
30
27
|
|
28
|
+
# Gets a specific asset.
|
29
|
+
# Takes an id of space and asset.
|
30
|
+
# Returns a Contentful::Management::Asset.
|
31
31
|
def self.find(space_id, asset_id)
|
32
32
|
request = Request.new("/#{ space_id }/assets/#{ asset_id }")
|
33
33
|
response = request.get
|
@@ -35,47 +35,60 @@ module Contentful
|
|
35
35
|
result.run
|
36
36
|
end
|
37
37
|
|
38
|
+
# Creates an asset.
|
39
|
+
# Takes a space id and hash with attributes (title, description, file)
|
40
|
+
# Returns a Contentful::Management::Asset.
|
38
41
|
def self.create(space_id, attributes)
|
39
|
-
asset =
|
42
|
+
asset = new
|
40
43
|
asset.instance_variable_set(:@fields, attributes[:fields] || {})
|
41
44
|
asset.locale = attributes[:locale] if attributes[:locale]
|
42
45
|
asset.title = attributes[:title] if attributes[:title]
|
43
46
|
asset.description = attributes[:description] if attributes[:description]
|
44
47
|
asset.file = attributes[:file] if attributes[:file]
|
45
48
|
|
46
|
-
request = Request.new("/#{ space_id }/assets/#{ attributes[:id] || ''}", {
|
49
|
+
request = Request.new("/#{ space_id }/assets/#{ attributes[:id] || ''}", {fields: asset.fields_for_query})
|
47
50
|
response = attributes[:id].nil? ? request.post : request.put
|
48
51
|
result = ResourceBuilder.new(self, response, {}, {}).run
|
49
52
|
result.process_files if result.is_a? self
|
50
53
|
result
|
51
54
|
end
|
52
55
|
|
56
|
+
# This method is used only when an asset is created. Processes the uploaded file.
|
53
57
|
def process_files
|
54
58
|
instance_variable_get(:@fields).keys.each do |locale|
|
55
59
|
request = Request.new("/#{ space.id }/assets/#{ id }/files/#{ locale }/process", {}, id = nil, version: sys[:version])
|
56
60
|
request.put
|
57
61
|
end
|
62
|
+
sys[:version] += 1
|
63
|
+
self
|
58
64
|
end
|
59
65
|
|
66
|
+
# Updates an asset.
|
67
|
+
# Takes hash with attributes (title, description, file)
|
68
|
+
# Returns a Contentful::Management::Asset.
|
60
69
|
def update(attributes)
|
61
70
|
self.title = attributes[:title] if attributes[:title]
|
62
71
|
self.description = attributes[:description] if attributes[:description]
|
63
72
|
self.file = attributes[:file] if attributes[:file]
|
64
|
-
request = Request.new("/#{ space.id }/assets/#{ id }", {
|
73
|
+
request = Request.new("/#{ space.id }/assets/#{ id }", {fields: fields_for_query}, id = nil, version: sys[:version])
|
65
74
|
response = request.put
|
66
75
|
result = ResourceBuilder.new(self, response, {}, {}).run
|
67
76
|
refresh_data(result)
|
68
77
|
end
|
69
78
|
|
79
|
+
# If an asset is a new object gets created in the Contentful, otherwise the existing asset gets updated.
|
80
|
+
# See README for details.
|
70
81
|
def save
|
71
82
|
if id.nil?
|
72
|
-
new_instance = self.class.create(self.sys[:space].id, {
|
83
|
+
new_instance = self.class.create(self.sys[:space].id, {fields: instance_variable_get(:@fields)})
|
73
84
|
refresh_data(new_instance)
|
74
85
|
else
|
75
86
|
update(title: title, description: description, file: file)
|
76
87
|
end
|
77
88
|
end
|
78
89
|
|
90
|
+
# Destroys an asset.
|
91
|
+
# Returns true if succeed.
|
79
92
|
def destroy
|
80
93
|
request = Request.new("/#{ space.id }/assets/#{ id }")
|
81
94
|
response = request.delete
|
@@ -87,6 +100,8 @@ module Contentful
|
|
87
100
|
end
|
88
101
|
end
|
89
102
|
|
103
|
+
# Publishes an asset.
|
104
|
+
# Returns a Contentful::Management::Asset.
|
90
105
|
def publish
|
91
106
|
request = Request.new("/#{ space.id }/assets/#{ id }/published", {}, id = nil, version: sys[:version])
|
92
107
|
response = request.put
|
@@ -94,6 +109,8 @@ module Contentful
|
|
94
109
|
refresh_data(result)
|
95
110
|
end
|
96
111
|
|
112
|
+
# Unpublishes an asset.
|
113
|
+
# Returns a Contentful::Management::Asset.
|
97
114
|
def unpublish
|
98
115
|
request = Request.new("/#{ space.id }/assets/#{ id }/published", {}, id = nil, version: sys[:version])
|
99
116
|
response = request.delete
|
@@ -101,6 +118,8 @@ module Contentful
|
|
101
118
|
refresh_data(result)
|
102
119
|
end
|
103
120
|
|
121
|
+
# Archive an asset.
|
122
|
+
# Returns a Contentful::Management::Asset.
|
104
123
|
def archive
|
105
124
|
request = Request.new("/#{ space.id }/assets/#{ id }/archived", {}, id = nil, version: sys[:version])
|
106
125
|
response = request.put
|
@@ -108,6 +127,8 @@ module Contentful
|
|
108
127
|
refresh_data(result)
|
109
128
|
end
|
110
129
|
|
130
|
+
# Unarchvie an asset.
|
131
|
+
# Returns a Contentful::Management::Asset.
|
111
132
|
def unarchive
|
112
133
|
request = Request.new("/#{ space.id }/assets/#{ id }/archived", {}, id = nil, version: sys[:version])
|
113
134
|
response = request.delete
|
@@ -115,18 +136,24 @@ module Contentful
|
|
115
136
|
refresh_data(result)
|
116
137
|
end
|
117
138
|
|
139
|
+
# Checks if an asset is published.
|
140
|
+
# Returns true if published.
|
118
141
|
def published?
|
119
142
|
!sys[:publishedAt].nil?
|
120
143
|
end
|
121
144
|
|
145
|
+
# Checks if an asset is archvied.
|
146
|
+
# Returns true if archived.
|
122
147
|
def archived?
|
123
148
|
!sys[:archivedAt].nil?
|
124
149
|
end
|
125
150
|
|
151
|
+
# Returns currently supported local or default locale.
|
126
152
|
def locale
|
127
153
|
sys && sys[:locale] ? sys[:locale] : default_locale
|
128
154
|
end
|
129
155
|
|
156
|
+
# Parser for assets attributes, creates appropriate form of request.
|
130
157
|
def fields_for_query
|
131
158
|
self.class.fields_coercions.keys.each_with_object({}) do |field_name, results|
|
132
159
|
results[field_name] = @fields.each_with_object({}) do |(locale, fields), field_results|
|
@@ -134,30 +161,6 @@ module Contentful
|
|
134
161
|
end
|
135
162
|
end
|
136
163
|
end
|
137
|
-
|
138
|
-
# Returns the image url of an asset
|
139
|
-
# Allows you to pass in the following options for image resizing:
|
140
|
-
# :width
|
141
|
-
# :height
|
142
|
-
# :format
|
143
|
-
# :quality
|
144
|
-
# See https://www.contentful.com/developers/documentation/content-delivery-api/#image-asset-resizing
|
145
|
-
def image_url(options = {})
|
146
|
-
query = {
|
147
|
-
w: options[:w] || options[:width],
|
148
|
-
h: options[:h] || options[:height],
|
149
|
-
fm: options[:fm] || options[:format],
|
150
|
-
q: options[:q] || options[:quality]
|
151
|
-
}.reject { |_k, v| v.nil? }
|
152
|
-
|
153
|
-
if query.empty?
|
154
|
-
file.url
|
155
|
-
else
|
156
|
-
"#{ file.url }?#{ URI.encode_www_form(query) }"
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
164
|
end
|
161
165
|
end
|
162
166
|
end
|
163
|
-
|
@@ -42,7 +42,7 @@ module Contentful
|
|
42
42
|
# Use this method together with the client's :dynamic_entries configuration.
|
43
43
|
# See README for details.
|
44
44
|
def update_dynamic_entry_cache_for_space!(space)
|
45
|
-
update_dynamic_entry_cache!(space.content_types)
|
45
|
+
update_dynamic_entry_cache!(space.content_types.all)
|
46
46
|
end
|
47
47
|
|
48
48
|
def update_dynamic_entry_cache!(content_types)
|
@@ -72,9 +72,16 @@ module Contentful
|
|
72
72
|
request_url = request.url
|
73
73
|
url = request.absolute? ? request_url : base_url + request_url
|
74
74
|
raw_response = yield(url)
|
75
|
+
clear_headers
|
75
76
|
Response.new(raw_response, request)
|
76
77
|
end
|
77
78
|
|
79
|
+
def clear_headers
|
80
|
+
self.content_type_id = nil
|
81
|
+
self.version = nil
|
82
|
+
self.organization_id = nil
|
83
|
+
end
|
84
|
+
|
78
85
|
def delete(request)
|
79
86
|
execute_request(request) do |url|
|
80
87
|
self.class.delete_http(url, {}, request_headers)
|
@@ -4,8 +4,9 @@ require_relative 'field'
|
|
4
4
|
|
5
5
|
module Contentful
|
6
6
|
module Management
|
7
|
+
# Resource class for ContentType.
|
8
|
+
# https://www.contentful.com/developers/documentation/content-management-api/#resources-content-types
|
7
9
|
class ContentType
|
8
|
-
|
9
10
|
FIELD_TYPES = [
|
10
11
|
SYMBOL = 'Symbol',
|
11
12
|
TEXT = 'Text',
|
@@ -26,8 +27,10 @@ module Contentful
|
|
26
27
|
property :name, :string
|
27
28
|
property :description, :string
|
28
29
|
property :fields, Field
|
29
|
-
property :displayField, :string
|
30
30
|
|
31
|
+
# Gets a collection of content types.
|
32
|
+
# Takes an id of space.
|
33
|
+
# Returns a Contentful::Management::Array of Contentful::Management::ContentType.
|
31
34
|
def self.all(space_id)
|
32
35
|
request = Request.new("/#{ space_id }/content_types")
|
33
36
|
response = request.get
|
@@ -37,6 +40,9 @@ module Contentful
|
|
37
40
|
content_types
|
38
41
|
end
|
39
42
|
|
43
|
+
# Gets a specific entry.
|
44
|
+
# Takes an id of space and content type.
|
45
|
+
# Returns a Contentful::Management::ContentType.
|
40
46
|
def self.find(space_id, content_type_id)
|
41
47
|
request = Request.new("/#{ space_id }/content_types/#{ content_type_id }")
|
42
48
|
response = request.get
|
@@ -46,6 +52,8 @@ module Contentful
|
|
46
52
|
content_type
|
47
53
|
end
|
48
54
|
|
55
|
+
# Destroys a content type.
|
56
|
+
# Returns true if succeed.
|
49
57
|
def destroy
|
50
58
|
request = Request.new("/#{ space.id }/content_types/#{ id }")
|
51
59
|
response = request.delete
|
@@ -57,6 +65,8 @@ module Contentful
|
|
57
65
|
end
|
58
66
|
end
|
59
67
|
|
68
|
+
# Activates a content type.
|
69
|
+
# Returns a Contentful::Management::ContentType.
|
60
70
|
def activate
|
61
71
|
request = Request.new("/#{ space.id }/content_types/#{ id }/published", {}, id = nil, version: sys[:version])
|
62
72
|
response = request.put
|
@@ -64,6 +74,9 @@ module Contentful
|
|
64
74
|
refresh_data(result)
|
65
75
|
end
|
66
76
|
|
77
|
+
# Deactivates a content type.
|
78
|
+
# Only content type that has no entries can be deactivated.
|
79
|
+
# Returns a Contentful::Management::ContentType.
|
67
80
|
def deactivate
|
68
81
|
request = Request.new("/#{ space.id }/content_types/#{ id }/published")
|
69
82
|
response = request.delete
|
@@ -71,10 +84,15 @@ module Contentful
|
|
71
84
|
refresh_data(result)
|
72
85
|
end
|
73
86
|
|
87
|
+
# Checks if a content type is active.
|
88
|
+
# Returns true if active.
|
74
89
|
def active?
|
75
90
|
!sys[:publishedAt].nil?
|
76
91
|
end
|
77
92
|
|
93
|
+
# Creates a content type.
|
94
|
+
# Takes an id of space and hash with attributes.
|
95
|
+
# Returns a Contentful::Management::ContentType.
|
78
96
|
def self.create(space_id, attributes)
|
79
97
|
fields = fields_to_nested_properties_hash(attributes[:fields] || [])
|
80
98
|
request = Request.new("/#{ space_id }/content_types/#{ attributes[:id] || ''}", { name: attributes.fetch(:name),
|
@@ -86,8 +104,12 @@ module Contentful
|
|
86
104
|
result
|
87
105
|
end
|
88
106
|
|
107
|
+
# Updates a content type.
|
108
|
+
# Takes a hash with attributes.
|
109
|
+
# Returns a Contentful::Management::ContentType.
|
89
110
|
def update(attributes)
|
90
111
|
parameters = {}
|
112
|
+
parameters.merge!(displayField: attributes[:displayField]) if attributes[:displayField]
|
91
113
|
parameters.merge!(name: (attributes[:name] || name))
|
92
114
|
parameters.merge!(description: (attributes[:description] || description))
|
93
115
|
parameters.merge!(fields: self.class.fields_to_nested_properties_hash(attributes[:fields] || fields))
|
@@ -97,6 +119,8 @@ module Contentful
|
|
97
119
|
refresh_data(result)
|
98
120
|
end
|
99
121
|
|
122
|
+
# If a content type is a new object gets created in the Contentful, otherwise the existing entry gets updated.
|
123
|
+
# See README for details.
|
100
124
|
def save
|
101
125
|
if id.nil?
|
102
126
|
new_instance = self.class.create(space.id, @properties)
|
@@ -106,6 +130,7 @@ module Contentful
|
|
106
130
|
end
|
107
131
|
end
|
108
132
|
|
133
|
+
# This method merges existing fields with new one, when adding, creating or updating new fields.
|
109
134
|
def merged_fields(new_field)
|
110
135
|
field_ids = []
|
111
136
|
merged_fields = fields.each_with_object([]) do |field, fields|
|
@@ -119,6 +144,10 @@ module Contentful
|
|
119
144
|
|
120
145
|
alias_method :orig_fields, :fields
|
121
146
|
|
147
|
+
# Use this method only in the context of content type.
|
148
|
+
# Allows you to add and create a field with specified attributes or destroy by pass field id.
|
149
|
+
# Returns a Contentful::Management::ContentType.
|
150
|
+
# See README for details.
|
122
151
|
def fields
|
123
152
|
fields = orig_fields
|
124
153
|
|
@@ -150,15 +179,25 @@ module Contentful
|
|
150
179
|
fields
|
151
180
|
end
|
152
181
|
|
153
|
-
#
|
182
|
+
# Use this method only in the context of content type.
|
183
|
+
# Allows you to create an entry.
|
184
|
+
# Returns a Contentful::Management::Entry.
|
185
|
+
# See README for details.
|
154
186
|
def entries
|
155
|
-
entries =
|
187
|
+
entries = nil
|
188
|
+
|
156
189
|
entries.instance_exec(self) do |content_type|
|
190
|
+
|
191
|
+
define_singleton_method(:all) do
|
192
|
+
Contentful::Management::Entry.all(content_type.space.id, content_type_id: content_type.id)
|
193
|
+
end
|
194
|
+
|
157
195
|
define_singleton_method(:create) do |params|
|
158
196
|
Entry.create(content_type, params)
|
159
197
|
end
|
198
|
+
|
160
199
|
define_singleton_method(:new) do
|
161
|
-
dynamic_entry_class =
|
200
|
+
dynamic_entry_class = Contentful::Management::Client.shared_instance.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type))
|
162
201
|
dynamic_entry = dynamic_entry_class.new
|
163
202
|
dynamic_entry.content_type = content_type
|
164
203
|
dynamic_entry
|
@@ -174,7 +213,6 @@ module Contentful
|
|
174
213
|
field.properties.replace(field.properties_to_hash)
|
175
214
|
end
|
176
215
|
end
|
177
|
-
|
178
216
|
end
|
179
217
|
end
|
180
218
|
end
|