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 +4 -4
- data/.rubocop_todo.yml +13 -11
- data/.ruby-version +1 -1
- data/.travis.yml +2 -2
- data/Gemfile.lock +18 -19
- data/README.md +70 -48
- data/consyncful.gemspec +2 -2
- data/lib/consyncful/base.rb +9 -3
- data/lib/consyncful/item_mapper.rb +49 -25
- data/lib/consyncful/persisted_item.rb +3 -1
- data/lib/consyncful/railtie.rb +1 -0
- data/lib/consyncful/stats.rb +2 -0
- data/lib/consyncful/sync.rb +18 -2
- data/lib/consyncful/tasks/consyncful.rake +1 -1
- data/lib/consyncful/version.rb +1 -1
- data/lib/consyncful.rb +21 -16
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 427a251dcb99af926beb09b1bd9c606319df5107c65954f152d007d5d3efc10c
|
4
|
+
data.tar.gz: d6774652c16586f99ed13a9683c5fa800fdd5e1535bb6c117d280f941d82a37f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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:
|
20
|
+
# Offense count: 10
|
14
21
|
# Configuration parameters: CountComments, ExcludedMethods.
|
15
22
|
# ExcludedMethods: refine
|
16
23
|
Metrics/BlockLength:
|
17
|
-
Max:
|
24
|
+
Max: 175
|
18
25
|
|
19
|
-
# Offense count:
|
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:
|
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:
|
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.
|
1
|
+
2.7.1
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
consyncful (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.
|
14
|
-
activesupport (= 6.
|
15
|
-
activesupport (6.
|
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 (>=
|
18
|
-
minitest (
|
19
|
-
tzinfo (~>
|
20
|
-
zeitwerk (~> 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.
|
24
|
+
bson (4.12.0)
|
25
25
|
concurrent-ruby (1.1.8)
|
26
|
-
contentful (2.
|
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.
|
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.
|
49
|
+
i18n (1.8.10)
|
50
50
|
concurrent-ruby (~> 1.0)
|
51
51
|
jaro_winkler (1.5.4)
|
52
|
-
minitest (5.14.
|
52
|
+
minitest (5.14.4)
|
53
53
|
mongo (2.14.0)
|
54
54
|
bson (>= 4.8.2, < 5.0.0)
|
55
|
-
mongoid (7.2.
|
56
|
-
activemodel (>= 5.1, < 6.
|
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
|
-
|
87
|
-
|
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.
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
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
|
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
|
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
|
-
###
|
93
|
+
### Synchronizing contentful data
|
85
94
|
|
86
|
-
To run a
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
135
|
+
instance.other_things.in_order # ordered the same as in Contentful
|
127
136
|
```
|
128
137
|
|
129
|
-
#### Finding
|
138
|
+
#### Finding entries from different content types
|
130
139
|
|
131
|
-
Because all
|
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
|
-
|
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
|
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
|
-
|
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
|
-
###
|
179
|
+
### Why do I have to use MongoDB?
|
158
180
|
|
159
|
-
|
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'
|
data/lib/consyncful/base.rb
CHANGED
@@ -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
|
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
|
-
|
15
|
-
|
16
|
-
|
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(
|
27
|
+
def mapped_fields(default_locale)
|
26
28
|
fields = generic_fields
|
27
29
|
|
28
|
-
|
29
|
-
|
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
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
53
|
-
|
54
|
-
|
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
|
93
|
+
def mapped_field_entry_for(field, value)
|
70
94
|
if single_reference?(value)
|
71
|
-
|
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
|
-
|
98
|
+
[ids_field_name.to_sym, value.map(&:id)]
|
75
99
|
else
|
76
|
-
|
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
|
-
|
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
|
|
data/lib/consyncful/railtie.rb
CHANGED
data/lib/consyncful/stats.rb
CHANGED
data/lib/consyncful/sync.rb
CHANGED
@@ -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
|
22
|
+
if Rails.autoloaders.zeitwerk_enabled?
|
23
23
|
Zeitwerk::Loader.eager_load_all
|
24
24
|
else
|
25
25
|
Rails.application.eager_load!
|
data/lib/consyncful/version.rb
CHANGED
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
|
-
|
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-
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
+
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:
|
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
|
-
|
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
|