consyncful 0.4.0 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5798e871965ab5ca699b12f71a397ef37025d75547786ea804e1c38a9b0091ac
4
- data.tar.gz: abfc466d422b44adc07abdc2d1b6bc36ff6e3dde8b57006d856bd809aaae86d5
3
+ metadata.gz: 427a251dcb99af926beb09b1bd9c606319df5107c65954f152d007d5d3efc10c
4
+ data.tar.gz: d6774652c16586f99ed13a9683c5fa800fdd5e1535bb6c117d280f941d82a37f
5
5
  SHA512:
6
- metadata.gz: 8dbb9580e3aaa1aee124608323d877ab21f89da591976394bedd69c400682f88714b83598a14bacf32c18987089bfd84225273d2c868a4c17a033bb366c0ff3d
7
- data.tar.gz: e3dc1aaf34278f60014f30c7f6487ba4558a7a0c9810ece3b44ee61e387a2036e52bb4eeb8955a94297a800f87ee2f6a5dedc85e26fc24d3e1bc91e256a6fbe7
6
+ metadata.gz: d7d2852b4f286dc1b19557f1e59411c3d4a4f354f1e4466ff14596a425fb7cb1f1cb858ab0fb61229004884442adb6aef44661be150bb10f10f18782cc79bc52
7
+ data.tar.gz: 8011d57e5ac51fe2a2c287e57d9adcd747de95858074734135ebe4b0e05ffd6b1c16bd2dca1840886978ce5ae0925d7d1a28d15e5c1bc0c2c53a606760c8899c
data/.rubocop_todo.yml CHANGED
@@ -1,22 +1,29 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2020-01-24 14:47:44 +1300 using RuboCop version 0.79.0.
3
+ # on 2021-03-01 10:54:42 +1300 using RuboCop version 0.79.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
+ # Offense count: 1
10
+ # Cop supports --auto-correct.
11
+ # Configuration parameters: AllowInHeredoc.
12
+ Layout/TrailingWhitespace:
13
+ Exclude:
14
+ - 'lib/consyncful/item_mapper.rb'
15
+
9
16
  # Offense count: 1
10
17
  Metrics/AbcSize:
11
18
  Max: 19
12
19
 
13
- # Offense count: 7
20
+ # Offense count: 10
14
21
  # Configuration parameters: CountComments, ExcludedMethods.
15
22
  # ExcludedMethods: refine
16
23
  Metrics/BlockLength:
17
- Max: 153
24
+ Max: 175
18
25
 
19
- # Offense count: 4
26
+ # Offense count: 3
20
27
  # Cop supports --auto-correct.
21
28
  # Configuration parameters: AutoCorrect, EnforcedStyle.
22
29
  # SupportedStyles: nested, compact
@@ -24,21 +31,16 @@ Style/ClassAndModuleChildren:
24
31
  Exclude:
25
32
  - 'lib/consyncful/railtie.rb'
26
33
  - 'spec/consyncful/base_spec.rb'
27
- - 'spec/consyncful/sync_spec.rb'
28
34
 
29
- # Offense count: 7
35
+ # Offense count: 3
30
36
  Style/Documentation:
31
37
  Exclude:
32
38
  - 'spec/**/*'
33
39
  - 'test/**/*'
34
40
  - 'lib/consyncful.rb'
35
- - 'lib/consyncful/base.rb'
36
- - 'lib/consyncful/item_mapper.rb'
37
41
  - 'lib/consyncful/railtie.rb'
38
- - 'lib/consyncful/stats.rb'
39
- - 'lib/consyncful/sync.rb'
40
42
 
41
- # Offense count: 42
43
+ # Offense count: 56
42
44
  # Cop supports --auto-correct.
43
45
  # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
44
46
  # URISchemes: http, https
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.5.5
1
+ 2.7.1
data/.travis.yml CHANGED
@@ -5,10 +5,10 @@ cache: bundler
5
5
  rvm:
6
6
  - 2.5.1
7
7
  - 2.6.5
8
- before_install: gem install bundler -v 2.1.0
8
+ before_install: gem install bundler -v 2.1.4
9
9
  services:
10
10
  - mongodb
11
11
 
12
- script:
12
+ script:
13
13
  - bundle exec rake spec
14
14
  - bundle exec rubocop -P
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- consyncful (0.4.0)
4
+ consyncful (0.6.2)
5
5
  contentful (>= 2.11.1, < 3.0.0)
6
6
  hooks (>= 0.4.1)
7
7
  mongoid (>= 7.0.2, < 8.0.0)
@@ -10,27 +10,27 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- activemodel (6.0.3.4)
14
- activesupport (= 6.0.3.4)
15
- activesupport (6.0.3.4)
13
+ activemodel (6.1.3.1)
14
+ activesupport (= 6.1.3.1)
15
+ activesupport (6.1.3.1)
16
16
  concurrent-ruby (~> 1.0, >= 1.0.2)
17
- i18n (>= 0.7, < 2)
18
- minitest (~> 5.1)
19
- tzinfo (~> 1.1)
20
- zeitwerk (~> 2.2, >= 2.2.2)
17
+ i18n (>= 1.6, < 2)
18
+ minitest (>= 5.1)
19
+ tzinfo (~> 2.0)
20
+ zeitwerk (~> 2.3)
21
21
  addressable (2.7.0)
22
22
  public_suffix (>= 2.0.2, < 5.0)
23
23
  ast (2.4.0)
24
- bson (4.11.1)
24
+ bson (4.12.0)
25
25
  concurrent-ruby (1.1.8)
26
- contentful (2.15.4)
26
+ contentful (2.16.0)
27
27
  http (> 0.8, < 5.0)
28
28
  multi_json (~> 1)
29
29
  database_cleaner (1.8.3)
30
30
  diff-lcs (1.3)
31
31
  domain_name (0.5.20190701)
32
32
  unf (>= 0.0.5, < 1.0.0)
33
- ffi (1.14.2)
33
+ ffi (1.15.0)
34
34
  ffi-compiler (1.0.1)
35
35
  ffi (>= 1.0.0)
36
36
  rake
@@ -46,14 +46,14 @@ GEM
46
46
  http-form_data (2.3.0)
47
47
  http-parser (1.2.3)
48
48
  ffi-compiler (>= 1.0, < 2.0)
49
- i18n (1.8.7)
49
+ i18n (1.8.10)
50
50
  concurrent-ruby (~> 1.0)
51
51
  jaro_winkler (1.5.4)
52
- minitest (5.14.3)
52
+ minitest (5.14.4)
53
53
  mongo (2.14.0)
54
54
  bson (>= 4.8.2, < 5.0.0)
55
- mongoid (7.2.0)
56
- activemodel (>= 5.1, < 6.1)
55
+ mongoid (7.2.2)
56
+ activemodel (>= 5.1, < 6.2)
57
57
  mongo (>= 2.10.5, < 3.0.0)
58
58
  multi_json (1.15.0)
59
59
  parallel (1.19.1)
@@ -83,9 +83,8 @@ GEM
83
83
  ruby-progressbar (~> 1.7)
84
84
  unicode-display_width (>= 1.4.0, < 1.7)
85
85
  ruby-progressbar (1.10.1)
86
- thread_safe (0.3.6)
87
- tzinfo (1.2.9)
88
- thread_safe (~> 0.1)
86
+ tzinfo (2.0.4)
87
+ concurrent-ruby (~> 1.0)
89
88
  uber (0.0.15)
90
89
  unf (0.1.4)
91
90
  unf_ext
@@ -105,4 +104,4 @@ DEPENDENCIES
105
104
  rubocop (= 0.79.0)
106
105
 
107
106
  BUNDLED WITH
108
- 2.1.0
107
+ 2.1.4
data/README.md CHANGED
@@ -2,19 +2,27 @@
2
2
 
3
3
  Contentful -> local database synchronisation for Rails
4
4
 
5
- Requesting complicated models from the Contentful Delivery API in Rails applications is often
6
- too slow, and makes testing applications painful. Consyncful uses Contentful's syncronisation API
7
- to keep a local copy of the entire content in a Mongo database up to date.
8
-
9
- Once the content is availble locally, finding and interact with contentful data is as easy as
10
- using [Mongoid](https://docs.mongodb.com/mongoid/current/tutorials/mongoid-documents/) ODM.
11
-
12
- This gem doesn't provide any intergration with the management api or any way to update contentful models from the local store. It is strictly read only.
13
-
14
- ## Why do I have to use MongoDB?
15
-
16
- Consyncful currently only supports Mongoid ODM because models have dynamic schemas. And that's all we've had a chance to work out so far. :)
17
- The same pattern might be able to be extended to work with ActiveRecord, but having to migrate the local database as well as your contentful content type's seems tedious.
5
+ Requesting complicated models from the Contentful Delivery API in Rails applications is often too slow, and makes testing applications painful. Consyncful uses Contentful's synchronisation API to keep a local, up-to-date copy of the entire content in a Mongo database.
6
+
7
+ Once the content is available locally, finding and interact with contentful data is as easy as using [Mongoid](https://docs.mongodb.com/mongoid/current/tutorials/mongoid-documents/) ODM.
8
+
9
+ This gem doesn't provide any integration with the management API, or any way to update Contentful models from the local store. It is strictly read only.
10
+
11
+ - [Installation](#installation)
12
+ - [Usage](#usage)
13
+ - [Creating contentful models in your Rails app](#creating-contentful-models-in-your-rails-app)
14
+ - [Synchronizing contentful data](#synchronizing-contentful-data)
15
+ - [Finding and interacting with models](#finding-and-interacting-with-models)
16
+ - [Querying](#querying)
17
+ - [References](#references)
18
+ - [Finding entries from different content types](#finding-entries-from-different-content-types)
19
+ - [Sync callbacks](#sync-callbacks)
20
+ - [Using Locales for specific fields](#using-locales-for-specific-fields)
21
+ - [Configuring what Mongo database Consyncful uses](#configuring-what-mongo-database-consyncful-uses)
22
+ - [Why do I have to use MongoDB?](#why-do-i-have-to-use-mongodb)
23
+ - [Development](#development)
24
+ - [Contributing](#contributing)
25
+ - [License](#license)
18
26
 
19
27
  ## Installation
20
28
 
@@ -28,28 +36,29 @@ And then execute:
28
36
 
29
37
  $ bundle
30
38
 
31
- If you don't already use mongoid, generate a mongoid.yml by running:
39
+ If you don't already use Mongoid, generate a mongoid.yml by running:
32
40
 
33
41
  $ rake g mongoid:config
34
42
 
35
43
  Add an initializer:
36
- Consyncful uses [contentful.rb](https://github.com/contentful/contentful.rb) so client options are as documented there.
37
- ```ruby
38
- Consyncful.configure do |config|
39
- config.locale = 'en-NZ'
40
- config.contentful_client_options = {
41
- api_url: 'cdn.contentful.com',
42
- space: 'space_id',
43
- access_token: 'ACCESS TOKEN',
44
- environment: 'master', # optional
45
- logger: Logger.new(STDOUT) # optional for debugging
46
- }
47
- end
44
+
45
+ Consyncful uses [contentful.rb](https://github.com/contentful/contentful.rb); client options are as documented there.
46
+ ```rb
47
+ Consyncful.configure do |config|
48
+ config.locale = 'en-NZ'
49
+ config.contentful_client_options = {
50
+ api_url: 'cdn.contentful.com',
51
+ space: 'space_id',
52
+ access_token: 'ACCESS TOKEN',
53
+ environment: 'master', # optional
54
+ logger: Logger.new(STDOUT) # optional for debugging
55
+ }
56
+ end
48
57
  ```
49
58
 
50
59
  ## Usage
51
60
 
52
- ### Creating contentful models in your rails app
61
+ ### Creating contentful models in your Rails app
53
62
 
54
63
  Create models by inheriting from `Consyncful::Base`
55
64
 
@@ -59,9 +68,9 @@ class ModelName < Consyncful::Base
59
68
  end
60
69
  ```
61
70
 
62
- Model fields will be dynamicly assigned, but mongoid dynamic fields are not accessible if the entry has an empty field. If you want the accessor methods to be reliably available for fields it is recommended to define the fields in the model:
71
+ Model fields will be dynamically assigned, but Mongoid dynamic fields are not accessible if the entry has an empty field. If you want the accessor methods to be reliably available for fields it is recommended to define the fields in the model:
63
72
 
64
- ```ruby
73
+ ```ruby
65
74
  class ModelName < Consyncful::Base
66
75
  contentful_model_name 'contentfulTypeName'
67
76
 
@@ -70,9 +79,9 @@ class ModelName < Consyncful::Base
70
79
  end
71
80
  ```
72
81
 
73
- Contentful reference fields are a bit special compared with standard mongoid associations, Consyncful provides the following helpers to set up the correct relationships:
82
+ Contentful reference fields are a bit special compared with standard Mongoid associations. Consyncful provides the following helpers to set up the correct relationships:
74
83
 
75
- ```ruby
84
+ ```ruby
76
85
  class ModelWithReferences < Consyncful::Base
77
86
  contentful_model_name 'contentfulTypeName'
78
87
 
@@ -81,26 +90,26 @@ class ModelWithReferences < Consyncful::Base
81
90
  end
82
91
  ```
83
92
 
84
- ### Syncronizing contentful data
93
+ ### Synchronizing contentful data
85
94
 
86
- To run a syncronization process run:
95
+ To run a synchronization process run:
87
96
 
88
97
  $ rake consyncful:sync
89
98
 
90
- The first time you run this it will download all the contentful content, it will then check every 15 seconds for changes to the content and update/delete records in the database when changes are made in contentful.
99
+ The first time you run this it will download all the Contentful content. It will then check every 15 seconds for changes to the content and update/delete records in the database when changes are made in Contentful.
91
100
 
92
- If you want to syncronise from scratch run:
101
+ If you want to synchronise from scratch, run:
93
102
 
94
103
  $ rake consyncful:refresh
95
104
 
96
105
  It is recommended to refresh your data if you change model names.
97
106
 
98
- Now you've synced your data, it is all available via your rails models
107
+ Now you've synced your data, it is all available via your Rails models.
99
108
 
100
109
  ### Finding and interacting with models
101
110
 
102
111
  #### Querying
103
- Models are available using standard mongoid [queries](https://docs.mongodb.com/mongoid/current/tutorials/mongoid-queries/).
112
+ Models are available using standard Mongoid [queries](https://docs.mongodb.com/mongoid/current/tutorials/mongoid-queries/).
104
113
 
105
114
  ```ruby
106
115
  instance = ModelName.find_by(instance: 'foo')
@@ -109,7 +118,7 @@ instance.is_awesome # true
109
118
  ```
110
119
 
111
120
  #### References
112
- References work like you woule expect:
121
+ References work like you would expect:
113
122
 
114
123
  ```ruby
115
124
 
@@ -120,23 +129,23 @@ instance.other_things # all the referenced things, polymorphic, so might be diff
120
129
  ```
121
130
 
122
131
  **Except**:
123
- `references_many` associations return objects in a different order from how they are ordered in contentful. If you want them in the order they appare in contentful, use the `.in_order` helper:
132
+ `references_many` associations return objects in a different order from how they are ordered in Contentful. If you want them in the order they appear in Contentful, use the `.in_order` helper:
124
133
 
125
134
  ```ruby
126
- instance.other_things.in_order # ordered the same as in contentful
135
+ instance.other_things.in_order # ordered the same as in Contentful
127
136
  ```
128
137
 
129
- #### Finding entrys from different content types
138
+ #### Finding entries from different content types
130
139
 
131
- Because all contentful models are stored as polymorphic subtypes of Consyncful::Base, you can query all entries without knowing what type you are looking for:
140
+ Because all Contentful models are stored as polymorphic subtypes of `Consyncful::Base`, you can query all entries without knowing what type you are looking for:
132
141
 
133
142
  ```ruby
134
- Consyncful::Base.where(title: 'a title') # [ #<ModelName>, #<OtherModelName> ]
143
+ Consyncful::Base.where(title: 'a title') # [ #<ModelName>, #<OtherModelName> ]
135
144
  ```
136
145
 
137
- ### Sync callbacks
146
+ ### Sync callbacks
138
147
 
139
- You may want to attach some application logic to happen before or after a sync run, for example to update caches or something.
148
+ You may want to attach some application logic to happen before or after a sync run, for example to update caches.
140
149
 
141
150
  Callbacks can be registered using:
142
151
 
@@ -152,11 +161,24 @@ Consyncful::Sync.after_run do |updated_ids|
152
161
  end
153
162
  ```
154
163
 
155
- ## Limitations
164
+ ### Using Locales for specific fields
165
+
166
+ If fields have multiple locales then the default locale will be mapped to the field name. Additional locales will have a suffix (lower snake case) on the field name. e.g title (default), title_mi_nz (New Zealand Maori mi-NZ)
167
+
168
+ ### Configuring what Mongo database Consyncful uses
169
+
170
+ You can also configure what Mongoid client Consyncful uses and the name of the collection the entries are stored under. This is useful if you want to have your consyncful data hosted in a different mongo database than your application-specific mongo database.
171
+
172
+ ```rb
173
+ Consyncful.configure do |config|
174
+ config.mongo_client = :consyncful # defaults to :default (referencing the clients in mongoid.yml)
175
+ config.mongo_collection = 'contentful_models' # this is the default
176
+ end
177
+ ```
156
178
 
157
- ### Locales
179
+ ### Why do I have to use MongoDB?
158
180
 
159
- Current Consyncful only uses one globally configured locale to map the data to the database.
181
+ Consyncful currently only supports Mongoid ODM because models have dynamic schemas. And that's all we've had a chance to work out so far. The same pattern might be able to be extended to work with ActiveRecord, but having to migrate the local database as well as your contentful content type's seems tedious.
160
182
 
161
183
  ## Development
162
184
 
data/consyncful.gemspec CHANGED
@@ -7,8 +7,8 @@ require 'consyncful/version'
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = 'consyncful'
9
9
  spec.version = Consyncful::VERSION
10
- spec.authors = ['Andy Anastasiadis-Gray', 'Montgomery Anderson']
11
- spec.email = ['andy@boost.co.nz', 'montgomery@boost.co.nz']
10
+ spec.authors = ['Andy Anastasiadis-Gray', 'Montgomery Anderson', 'Greg Rogan']
11
+ spec.email = ['andy@boost.co.nz', 'montgomery@boost.co.nz', 'greg@boost.co.nz']
12
12
 
13
13
  spec.summary = 'Contentful to local database synchronisation for Rails'
14
14
  spec.homepage = 'https://github.com/boost/consyncful'
@@ -1,14 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Consyncful
4
+ ##
5
+ # Provides common functionality of Mongoid models created from contentful
6
+ # entries
4
7
  class Base
5
8
  include Mongoid::Document
6
9
  include Mongoid::Attributes::Dynamic
7
10
 
8
- store_in collection: 'contentful_models'
9
-
10
11
  cattr_accessor :model_map
11
12
 
13
+ store_in collection: -> { Consyncful.configuration.mongo_collection },
14
+ client: -> { Consyncful.configuration.mongo_client }
15
+
12
16
  def self.contentful_model_name(name)
13
17
  self.model_map ||= {}
14
18
 
@@ -19,7 +23,9 @@ module Consyncful
19
23
  def self.references_many(name)
20
24
  has_and_belongs_to_many name.to_sym, class_name: 'Consyncful::Base', inverse_of: nil do
21
25
  def in_order
22
- _target.to_a.sort_by { |a| _base[foreign_key].index(a.id) }
26
+ _target.to_a.sort_by do |reference|
27
+ _base[foreign_key].index(reference.id)
28
+ end
23
29
  end
24
30
  end
25
31
  end
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Consyncful
4
+ ##
5
+ # Responsible for mapping an update received from Contentful's syncronisation API
6
+ # into useful fields for Consyncful::PersistedItem to store in the database.
4
7
  class ItemMapper
5
8
  def initialize(item)
6
9
  @item = item
@@ -11,10 +14,9 @@ module Consyncful
11
14
  end
12
15
 
13
16
  def type
14
- if @item.type == 'Entry'
15
- @item.content_type.id
16
- elsif @item.type == 'Asset'
17
- 'asset'
17
+ case @item.type
18
+ when 'Entry' then @item.content_type.id
19
+ when 'Asset' then 'asset'
18
20
  end
19
21
  end
20
22
 
@@ -22,17 +24,11 @@ module Consyncful
22
24
  @item.id
23
25
  end
24
26
 
25
- def mapped_fields(locale)
27
+ def mapped_fields(default_locale)
26
28
  fields = generic_fields
27
29
 
28
- @item.fields_with_locales.each do |field, value_with_locales|
29
- value = value_with_locales[locale.to_sym]
30
- next if value.is_a? Contentful::File # it is special
31
-
32
- assign_field(fields, field, value)
33
- end
34
-
35
- fields[:file] = raw_file(locale) if type == 'asset'
30
+ fields.merge!(localized_fields(default_locale))
31
+ fields.merge!(localized_asset_fields(default_locale)) if type == 'asset'
36
32
 
37
33
  fields
38
34
  end
@@ -40,18 +36,46 @@ module Consyncful
40
36
  private
41
37
 
42
38
  def generic_fields
39
+ { created_at: @item.created_at,
40
+ updated_at: @item.updated_at,
41
+ revision: @item.revision,
42
+ contentful_type: type,
43
+ synced_at: Time.current }
44
+ end
45
+
46
+ def localized_fields(default_locale)
43
47
  fields = {}
44
- fields[:created_at] = @item.created_at
45
- fields[:updated_at] = @item.updated_at
46
- fields[:revision] = @item.revision
47
- fields[:contentful_type] = type
48
- fields[:synced_at] = Time.current
48
+
49
+ @item.fields_with_locales.each do |field, value_with_locales|
50
+ value_with_locales.each do |locale_code, value|
51
+ next if value.is_a? Contentful::File # assets are handeled below
52
+
53
+ field_name = localized_field_name(field, locale_code, default_locale)
54
+ field_name, value = mapped_field_entry_for(field_name, value)
55
+ fields[field_name] = value
56
+ end
57
+ end
58
+
49
59
  fields
50
60
  end
51
61
 
52
- def raw_file(locale)
53
- file_json = @item.raw.fetch('fields', {}).fetch('file', nil)
54
- file_json[locale] unless file_json.nil?
62
+ def localized_asset_fields(default_locale)
63
+ fields = {}
64
+ files_by_locale = @item.raw.dig('fields', 'file') || {}
65
+
66
+ files_by_locale.each do |locale_code, details|
67
+ field_name = localized_field_name('file', locale_code, default_locale)
68
+ fields[field_name.to_sym] = details
69
+ end
70
+
71
+ fields
72
+ end
73
+
74
+ # Suffixes the field with the locale unless it's the default locale.
75
+ def localized_field_name(field, locale_code, default_locale)
76
+ return field if locale_code.to_s == default_locale.to_s
77
+
78
+ "#{field}_#{locale_code.to_s.underscore}".to_sym
55
79
  end
56
80
 
57
81
  def reference_value?(value)
@@ -66,14 +90,14 @@ module Consyncful
66
90
  value.is_a?(Array) && single_reference?(value.first)
67
91
  end
68
92
 
69
- def assign_field(hash, field, value)
93
+ def mapped_field_entry_for(field, value)
70
94
  if single_reference?(value)
71
- hash[ActiveSupport::Inflector.foreign_key(field).to_sym] = value.id
95
+ [ActiveSupport::Inflector.foreign_key(field).to_sym, value.id]
72
96
  elsif many_reference?(value)
73
97
  ids_field_name = field.to_s.singularize + '_ids' # fk field name
74
- hash[ids_field_name.to_sym] = value.map(&:id)
98
+ [ids_field_name.to_sym, value.map(&:id)]
75
99
  else
76
- hash[field] = value
100
+ [field, value]
77
101
  end
78
102
  end
79
103
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Consyncful
4
- # Takes a mapped item from contentful and applies it to the local storage.
4
+ ##
5
+ # Takes a mapped item from Contentful and creates/updates/deletes
6
+ # the relevant model in the local database.
5
7
  class PersistedItem
6
8
  DEFAULT_LOCALE = 'en-NZ'
7
9
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Adds Consyncful task to Rails
3
4
  class Consyncful::Railtie < Rails::Railtie
4
5
  rake_tasks do
5
6
  load 'consyncful/tasks/consyncful.rake'
@@ -3,6 +3,8 @@
3
3
  require 'rainbow'
4
4
 
5
5
  module Consyncful
6
+ ##
7
+ # Responsible for recording changes during a sync for outputting in logs
6
8
  class Stats
7
9
  def initialize
8
10
  @stats = {
@@ -7,15 +7,23 @@ require 'consyncful/stats'
7
7
  require 'hooks'
8
8
 
9
9
  module Consyncful
10
+ ##
11
+ # A mongoid model that stores the state of a syncronisation feed. Stores the
12
+ # next URL provided by Contentfuls Sync API.
13
+ #
14
+ # Sync's are affectivly singletons,
15
+ # there should only ever be one in the database
16
+ #
17
+ # Is also the entrypoint of a Syncronization run
10
18
  class Sync
11
19
  include Mongoid::Document
12
20
  include Hooks
13
21
 
22
+ store_in client: -> { Consyncful.configuration.mongo_client }
23
+
14
24
  define_hook :before_run
15
25
  define_hook :after_run
16
26
 
17
- DEFAULT_LOCALE = 'en-NZ'
18
-
19
27
  field :next_url
20
28
  field :last_run_at, type: DateTime
21
29
 
@@ -23,17 +31,25 @@ module Consyncful
23
31
  last || new
24
32
  end
25
33
 
34
+ ##
35
+ # Delete the previous sync chains from database and create a fresh one.
36
+ # Used to completely resync all items from Contentful.
26
37
  def self.fresh
27
38
  destroy_all
28
39
  latest
29
40
  end
30
41
 
42
+ ##
43
+ # Makes sure that the database contains only records that have been provided
44
+ # during this chain of syncronisation.
31
45
  def drop_stale
32
46
  stale = Base.where(:sync_id.ne => id, :sync_id.exists => true)
33
47
  puts Rainbow("Dropping #{stale.count} records that haven't been touched in this sync").red
34
48
  stale.destroy
35
49
  end
36
50
 
51
+ ##
52
+ # Entry point to a syncronization run. Is responsible for updating Sync state
37
53
  def run
38
54
  run_hook :before_run
39
55
 
@@ -19,7 +19,7 @@ namespace :consyncful do
19
19
  end
20
20
 
21
21
  task update_model_names: [:environment] do
22
- if Object.const_defined?('Zeitwerk::Loader') && Rails.application.config.autoloader.to_s == 'zeitwerk'
22
+ if Rails.autoloaders.zeitwerk_enabled?
23
23
  Zeitwerk::Loader.eager_load_all
24
24
  else
25
25
  Rails.application.eager_load!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Consyncful
4
- VERSION = '0.4.0'
4
+ VERSION = '0.6.2'
5
5
  end
data/lib/consyncful.rb CHANGED
@@ -11,23 +11,18 @@ require 'consyncful/sync'
11
11
  require 'consyncful/railtie' if defined?(Rails)
12
12
 
13
13
  module Consyncful
14
- class << self
15
- attr_accessor :configuration
16
- end
17
-
18
- def self.configure
19
- self.configuration ||= Configuration.new
20
- yield(configuration)
21
- end
22
-
14
+ # Handles Rails configurations for Consynful
23
15
  class Configuration
24
- attr_accessor :contentful_client_options, :locale
16
+ attr_accessor :contentful_client_options, :locale,
17
+ :mongo_client, :mongo_collection
25
18
 
26
19
  def initialize
27
20
  @contentful_client_options = {
28
21
  api_url: 'cdn.contentful.com'
29
22
  }
30
- @locale = 'en-US'
23
+ @locale = 'en-NZ'
24
+ @mongo_client = :default
25
+ @mongo_collection = 'contentful_models'
31
26
  end
32
27
  end
33
28
 
@@ -36,11 +31,21 @@ module Consyncful
36
31
  api_url: 'cdn.contentful.com'
37
32
  }.freeze
38
33
 
39
- def self.client
40
- @client ||= begin
41
- options = Consyncful.configuration.contentful_client_options
42
- options.reverse_merge!(DEFAULT_CLIENT_OPTIONS)
43
- Contentful::Client.new(options)
34
+ class << self
35
+ def configuration
36
+ @configuration ||= Configuration.new
37
+ end
38
+
39
+ def configure
40
+ yield configuration
41
+ end
42
+
43
+ def client
44
+ @client ||= begin
45
+ options = Consyncful.configuration.contentful_client_options
46
+ options.reverse_merge!(DEFAULT_CLIENT_OPTIONS)
47
+ Contentful::Client.new(options)
48
+ end
44
49
  end
45
50
  end
46
51
  end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consyncful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Anastasiadis-Gray
8
8
  - Montgomery Anderson
9
+ - Greg Rogan
9
10
  autorequire:
10
11
  bindir: exe
11
12
  cert_chain: []
12
- date: 2021-02-03 00:00:00.000000000 Z
13
+ date: 2022-04-28 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: bundler
@@ -153,6 +154,7 @@ description:
153
154
  email:
154
155
  - andy@boost.co.nz
155
156
  - montgomery@boost.co.nz
157
+ - greg@boost.co.nz
156
158
  executables: []
157
159
  extensions: []
158
160
  extra_rdoc_files: []
@@ -201,8 +203,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
203
  - !ruby/object:Gem::Version
202
204
  version: '0'
203
205
  requirements: []
204
- rubyforge_project:
205
- rubygems_version: 2.7.6.2
206
+ rubygems_version: 3.1.2
206
207
  signing_key:
207
208
  specification_version: 4
208
209
  summary: Contentful to local database synchronisation for Rails