contentful_model 1.0.1 → 1.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb068dfa34793542d2494fdb81949a5bbc91a655fe9766bd7cc557574c81fd87
4
- data.tar.gz: 2656cb0fa65053912a96d75ac39850cb2df0a0cea4cc6abbeb69a2dae90d90d4
3
+ metadata.gz: 124ae1820078574b614c6cd41c9ba657222d75209637d1d0090ebd05aa367864
4
+ data.tar.gz: f309c31e27596c7d90954cf0d5d18112ed09c355eb8ed475591274174eaa8b7b
5
5
  SHA512:
6
- metadata.gz: 34f7dbb45f0a8bb0e7318dba146582ac767c2268a6963fb67fe7a1bce0feaabc98653f8abb1753654e15ca0862c74b7cb77add86854b49373cecf91c545aa133
7
- data.tar.gz: 346c64b379119d12e8f58c2d61c59543c7b4315cb21ea64cf07c5caf7538e6586fa024c94bdb89e6fc559057ff6ccb85f7ed32fd77bd81047b4e751c1df597f9
6
+ metadata.gz: 1cd57a8ee48783aa9f5e1f91f779ac399446e563f9f6babf90e11c0c7801ee0b5cd9338ecd9b4b1d8e1fb838f9b48a641531137105ce906dd97400c75c9883ef
7
+ data.tar.gz: 527a17a0771d104b41c22d45db937f442f605acff40714dce57725b0ce728ea9af23de82e9896a7c22845ff0dd8d86b1e4cd92c31591b2286ec4885b6a2d6c55
@@ -25,8 +25,8 @@ module ContentfulModel
25
25
  # to write your own recursion for this, because it's probably an implementation-specific problem.
26
26
  # rubocop:disable Style/PredicateName
27
27
  def has_many_nested(association_name, options = {})
28
- has_many association_name, class_name: to_s, inverse_of: :"parent_#{to_s.underscore}"
29
- belongs_to_many :"parent_#{to_s.underscore.pluralize}", class_name: to_s
28
+ has_many association_name, class_name: to_s, inverse_of: :"parent_#{has_many_nested_name}"
29
+ belongs_to_many :"parent_#{has_many_nested_name.pluralize}", class_name: to_s
30
30
  root_method = options[:root] if options[:root].is_a?(Proc)
31
31
 
32
32
  # If there's a root method defined, set up a class method called root_[class name]. In our example this would be
@@ -34,14 +34,14 @@ module ContentfulModel
34
34
  # @return [Object] the root entity returned from the proc defined in has_many_nested
35
35
  if defined?(root_method) && root_method.is_a?(Proc)
36
36
  # @return [Object] the root entity
37
- define_method :"root_#{to_s.underscore}" do
37
+ define_method :"root_#{has_many_nested_name}" do
38
38
  root_method.call
39
39
  end
40
40
  end
41
41
 
42
42
  # A utility method which returns the parent object; saves passing around interpolated strings
43
43
  define_method :parent do
44
- parents = send(:"parent_#{self.class.to_s.underscore.pluralize}")
44
+ parents = send(:"parent_#{self.class.has_many_nested_name.pluralize}")
45
45
  parents.first unless parents.nil?
46
46
  end
47
47
 
@@ -131,6 +131,10 @@ module ContentfulModel
131
131
  end
132
132
  send(:private, :flatten_hash)
133
133
  end
134
+
135
+ def has_many_nested_name
136
+ to_s.demodulize.underscore
137
+ end
134
138
  # rubocop:enable Style/PredicateName
135
139
  end
136
140
  end
@@ -18,7 +18,8 @@ module ContentfulModel
18
18
  raise_errors: true,
19
19
  dynamic_entries: :auto,
20
20
  integration_name: 'contentful_model',
21
- integration_version: ::ContentfulModel::VERSION
21
+ integration_version: ::ContentfulModel::VERSION,
22
+ raise_for_empty_fields: false
22
23
  }.merge(configuration))
23
24
  end
24
25
  end
@@ -4,7 +4,12 @@ module ContentfulModel
4
4
  module Migrations
5
5
  # Class for defining Content Type transformations
6
6
  class ContentType
7
- attr_accessor :id, :name
7
+ attr_accessor :id, :name, :display_field
8
+
9
+ MANAGEMENT_TYPE_MAPPING = {
10
+ 'string' => 'Symbol',
11
+ 'rich_text' => 'RichText'
12
+ }.freeze
8
13
 
9
14
  def initialize(name = nil, management_content_type = nil)
10
15
  @name = name
@@ -19,10 +24,12 @@ module ContentfulModel
19
24
  ).create(
20
25
  id: id || camel_case(@name),
21
26
  name: @name,
27
+ displayField: display_field,
22
28
  fields: fields
23
29
  )
24
30
  else
25
31
  @management_content_type.fields = @fields
32
+ @management_content_type.displayField = display_field if display_field
26
33
  @management_content_type.save
27
34
  end
28
35
 
@@ -78,12 +85,12 @@ module ContentfulModel
78
85
  def management_type(type)
79
86
  if %i[text symbol integer number date boolean location object].include?(type.to_sym)
80
87
  type.capitalize
81
- elsif type == 'string'
82
- 'Symbol'
83
88
  elsif link?(type)
84
89
  'Link'
85
90
  elsif array?(type)
86
91
  'Array'
92
+ elsif MANAGEMENT_TYPE_MAPPING.key?(type.to_s)
93
+ MANAGEMENT_TYPE_MAPPING[type.to_s]
87
94
  else
88
95
  raise_field_type_error(type)
89
96
  end
@@ -1,3 +1,3 @@
1
1
  module ContentfulModel
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
data/spec/base_spec.rb CHANGED
@@ -100,6 +100,7 @@ describe ContentfulModel::Base do
100
100
  nyancat = CoercedCat.find('nyancat')
101
101
  expect(nyancat.name).to eq 'Fat Cat'
102
102
  expect(nyancat.created_at).to eq '2013-06-27T22:46:19+00:00'
103
+ expect(nyancat.updated_at).to be_instance_of(DateTime)
103
104
  }
104
105
  end
105
106
  end
@@ -30,22 +30,52 @@ describe ContentfulModel::Migrations::ContentType do
30
30
  end
31
31
  end
32
32
 
33
- it 'creates a new content type on contentful' do
34
- mock_client = Object.new
35
- expect(mock_client).to receive(:create).with(id: 'foo', name: 'foo', fields: [])
36
- allow_any_instance_of(ContentfulModel::Management).to receive(:content_types) { mock_client }
33
+ context 'with a new content type' do
34
+ let(:mock_client) { Object.new }
37
35
 
38
- described_class.new('foo').save
36
+ before do
37
+ allow_any_instance_of(ContentfulModel::Management).to receive(:content_types) { mock_client }
38
+ end
39
+
40
+ it 'creates it on contentful' do
41
+ expect(mock_client).to receive(:create).with(id: 'foo', name: 'foo', displayField: nil, fields: [])
42
+
43
+ described_class.new('foo').save
44
+ end
45
+
46
+ it 'allows to specify display field' do
47
+ expect(mock_client).to receive(:create).with(id: 'foo', name: 'foo', displayField: 'field_id', fields: [])
48
+
49
+ ct = described_class.new('foo')
50
+ ct.display_field = 'field_id'
51
+ ct.save
52
+ end
39
53
  end
40
54
 
41
- it 'updates an existing content type' do
42
- mock_ct = Object.new
43
- allow(mock_ct).to receive(:id) { 'foo' }
55
+ context 'with an existing content type' do
56
+ let(:mock_ct) { Object.new }
44
57
 
45
- expect(mock_ct).to receive(:fields=)
46
- expect(mock_ct).to receive(:save)
58
+ before do
59
+ allow(mock_ct).to receive(:id) { 'foo' }
60
+ end
61
+
62
+ it 'updates it' do
63
+ expect(mock_ct).to receive(:fields=)
64
+ expect(mock_ct).not_to receive(:displayField=)
65
+ expect(mock_ct).to receive(:save)
66
+
67
+ described_class.new(nil, mock_ct).save
68
+ end
47
69
 
48
- described_class.new(nil, mock_ct).save
70
+ it 'updates display field' do
71
+ expect(mock_ct).to receive(:fields=)
72
+ expect(mock_ct).to receive(:displayField=)
73
+ expect(mock_ct).to receive(:save)
74
+
75
+ ct = described_class.new(nil, mock_ct)
76
+ ct.display_field = 'field_id'
77
+ ct.save
78
+ end
49
79
  end
50
80
  end
51
81
 
@@ -71,6 +101,7 @@ describe ContentfulModel::Migrations::ContentType do
71
101
  expect_any_instance_of(::Contentful::Management::ClientContentTypeMethodsFactory).to receive(:create).with(
72
102
  id: 'foo_ct',
73
103
  name: subject.name,
104
+ displayField: nil,
74
105
  fields: subject.fields
75
106
  )
76
107
 
@@ -127,6 +158,12 @@ describe ContentfulModel::Migrations::ContentType do
127
158
  expect(items.link_type).to eq('Asset')
128
159
  end
129
160
 
161
+ it 'rich_text field' do
162
+ field = subject.field('foo', :rich_text)
163
+
164
+ expect(field.type).to eq('RichText')
165
+ end
166
+
130
167
  it 'fails on unknown type' do
131
168
  expect { subject.field('foo', :bar) }.to raise_error ContentfulModel::Migrations::InvalidFieldTypeError
132
169
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contentful_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Contentful GmbH (David Litvak Bruno)
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-08-01 00:00:00.000000000 Z
12
+ date: 2019-05-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: contentful
@@ -353,66 +353,65 @@ required_rubygems_version: !ruby/object:Gem::Requirement
353
353
  - !ruby/object:Gem::Version
354
354
  version: '0'
355
355
  requirements: []
356
- rubyforge_project:
357
- rubygems_version: 2.7.6
356
+ rubygems_version: 3.0.3
358
357
  signing_key:
359
358
  specification_version: 4
360
359
  summary: An ActiveModel-like wrapper for the Contentful SDKs
361
360
  test_files:
362
- - spec/asset_spec.rb
363
- - spec/associations/belongs_to_many_spec.rb
364
- - spec/associations/belongs_to_spec.rb
361
+ - spec/spec_helper.rb
362
+ - spec/management_spec.rb
363
+ - spec/migrations/content_type_spec.rb
364
+ - spec/migrations/migration_spec.rb
365
+ - spec/migrations/content_type_factory_spec.rb
365
366
  - spec/associations/has_many_nested_spec.rb
366
- - spec/associations/has_many_spec.rb
367
367
  - spec/associations/has_one_spec.rb
368
- - spec/base_spec.rb
369
- - spec/chainable_queries_spec.rb
368
+ - spec/associations/belongs_to_many_spec.rb
369
+ - spec/associations/has_many_spec.rb
370
+ - spec/associations/belongs_to_spec.rb
370
371
  - spec/client_spec.rb
372
+ - spec/validations/validations_spec.rb
373
+ - spec/chainable_queries_spec.rb
374
+ - spec/manageable_spec.rb
371
375
  - spec/contentful_model_spec.rb
372
- - spec/fixtures/vcr_cassettes/asset/all.yml
373
- - spec/fixtures/vcr_cassettes/asset/find.yml
376
+ - spec/fixtures/vcr_cassettes/associations/nested_without_root_middle_higher_include.yml
377
+ - spec/fixtures/vcr_cassettes/associations/nested_without_root_middle.yml
374
378
  - spec/fixtures/vcr_cassettes/association/belongs_to_many.yml
379
+ - spec/fixtures/vcr_cassettes/association/nested_without_root_middle_higher_include.yml
380
+ - spec/fixtures/vcr_cassettes/association/nested_without_root_child_parent.yml
375
381
  - spec/fixtures/vcr_cassettes/association/has_many.yml
376
382
  - spec/fixtures/vcr_cassettes/association/has_one.yml
377
- - spec/fixtures/vcr_cassettes/association/nested_with_root_root.yml
378
- - spec/fixtures/vcr_cassettes/association/nested_without_root_child_higher_include.yml
379
- - spec/fixtures/vcr_cassettes/association/nested_without_root_child_parent.yml
380
383
  - spec/fixtures/vcr_cassettes/association/nested_without_root_childless.yml
381
- - spec/fixtures/vcr_cassettes/association/nested_without_root_middle.yml
382
- - spec/fixtures/vcr_cassettes/association/nested_without_root_middle_higher_include.yml
384
+ - spec/fixtures/vcr_cassettes/association/nested_without_root_parentless_higher_include.yml
383
385
  - spec/fixtures/vcr_cassettes/association/nested_without_root_middle_parent.yml
384
386
  - spec/fixtures/vcr_cassettes/association/nested_without_root_parentless.yml
385
- - spec/fixtures/vcr_cassettes/association/nested_without_root_parentless_higher_include.yml
386
- - spec/fixtures/vcr_cassettes/associations/nested_without_root_middle.yml
387
- - spec/fixtures/vcr_cassettes/associations/nested_without_root_middle_higher_include.yml
388
- - spec/fixtures/vcr_cassettes/base/content_type.yml
389
- - spec/fixtures/vcr_cassettes/base/return_nil_for_empty.yml
390
- - spec/fixtures/vcr_cassettes/base/return_nil_for_empty_with_value.yml
387
+ - spec/fixtures/vcr_cassettes/association/nested_without_root_middle.yml
388
+ - spec/fixtures/vcr_cassettes/association/nested_without_root_child_higher_include.yml
389
+ - spec/fixtures/vcr_cassettes/association/nested_with_root_root.yml
390
+ - spec/fixtures/vcr_cassettes/asset/find.yml
391
+ - spec/fixtures/vcr_cassettes/asset/all.yml
392
+ - spec/fixtures/vcr_cassettes/playground/nyancat.yml
391
393
  - spec/fixtures/vcr_cassettes/client.yml
392
- - spec/fixtures/vcr_cassettes/dog.yml
393
- - spec/fixtures/vcr_cassettes/human.yml
394
+ - spec/fixtures/vcr_cassettes/management/nyancat_publish.yml
394
395
  - spec/fixtures/vcr_cassettes/management/client.yml
395
- - spec/fixtures/vcr_cassettes/management/nyancat.yml
396
396
  - spec/fixtures/vcr_cassettes/management/nyancat_2.yml
397
- - spec/fixtures/vcr_cassettes/management/nyancat_publish.yml
397
+ - spec/fixtures/vcr_cassettes/management/nyancat.yml
398
+ - spec/fixtures/vcr_cassettes/management/nyancat_save.yml
398
399
  - spec/fixtures/vcr_cassettes/management/nyancat_refetch_and_fail.yml
399
400
  - spec/fixtures/vcr_cassettes/management/nyancat_refetch_and_save.yml
400
- - spec/fixtures/vcr_cassettes/management/nyancat_save.yml
401
401
  - spec/fixtures/vcr_cassettes/management/nyancat_save_2.yml
402
402
  - spec/fixtures/vcr_cassettes/nyancat.yml
403
- - spec/fixtures/vcr_cassettes/playground/nyancat.yml
404
- - spec/fixtures/vcr_cassettes/query/each_entry.yml
405
- - spec/fixtures/vcr_cassettes/query/each_page.yml
403
+ - spec/fixtures/vcr_cassettes/human.yml
404
+ - spec/fixtures/vcr_cassettes/dog.yml
406
405
  - spec/fixtures/vcr_cassettes/query/empty.yml
407
- - spec/fixtures/vcr_cassettes/query/load.yml
406
+ - spec/fixtures/vcr_cassettes/query/each_page.yml
407
+ - spec/fixtures/vcr_cassettes/query/each_entry.yml
408
408
  - spec/fixtures/vcr_cassettes/query/manual_pagination.yml
409
+ - spec/fixtures/vcr_cassettes/query/load.yml
409
410
  - spec/fixtures/vcr_cassettes/query/nyancat_invalid_elements.yml
410
- - spec/manageable_spec.rb
411
- - spec/management_spec.rb
412
- - spec/migrations/content_type_factory_spec.rb
413
- - spec/migrations/content_type_spec.rb
414
- - spec/migrations/migration_spec.rb
411
+ - spec/fixtures/vcr_cassettes/base/return_nil_for_empty.yml
412
+ - spec/fixtures/vcr_cassettes/base/return_nil_for_empty_with_value.yml
413
+ - spec/fixtures/vcr_cassettes/base/content_type.yml
415
414
  - spec/queries_spec.rb
415
+ - spec/asset_spec.rb
416
416
  - spec/query_spec.rb
417
- - spec/spec_helper.rb
418
- - spec/validations/validations_spec.rb
417
+ - spec/base_spec.rb