consyncful 0.5.0 → 0.6.1

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: 14eda0769dbcdc516d59a9a4252fafb23eb6380ce0224a276e101f81d649c950
4
- data.tar.gz: aa4aaf421824043dbea600c1cfabca2633a8d3470ed2a14aba5685b8bf8acd90
3
+ metadata.gz: 6a8f36ac50a10800cbce6e6b9d086e346b1e7362e422080d532a87bb981d8135
4
+ data.tar.gz: 6df1ea6cf1cb05f405938d83e09436927e21b7828424f33c79a105e4c11da127
5
5
  SHA512:
6
- metadata.gz: 9ac1a66dda08d88396d83c736308516a798503afa519cd673e76f668d771dc933b6068a2e03c1f75926ec8980ad273c0936af6e2058d84d5919a250a498ebbd7
7
- data.tar.gz: 260c353fe5eb6c646473830ecdec0a094016bf0f0e518ef357ceb7cc03862f38e76e4cccbcf656a4c368fad72641ef60788d13a5c76e768a5b8fc9652b375c96
6
+ metadata.gz: bdebe27350c747d4d3d02e290c5971d2b295815741e887ad658a09248f0d7d0a798a4060d5500c99d2f18e4b7f920d98e32bc0f0d49bbdea488ded106bda53d7
7
+ data.tar.gz: 034ec0cbec2265225f48bbd52d6412fe238ee026b78827bfacec54e8a31c42f9786495709c7dcea1e4ecf261761897f99a8417e4bea09149296c6cc51c9f789b
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.5.0)
4
+ consyncful (0.6.1)
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
@@ -8,10 +8,21 @@ Once the content is available locally, finding and interact with contentful data
8
8
 
9
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
10
 
11
- ## Why do I have to use MongoDB?
12
-
13
- 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. :)
14
- 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.
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)
15
26
 
16
27
  ## Installation
17
28
 
@@ -32,17 +43,17 @@ If you don't already use Mongoid, generate a mongoid.yml by running:
32
43
  Add an initializer:
33
44
 
34
45
  Consyncful uses [contentful.rb](https://github.com/contentful/contentful.rb); client options are as documented there.
35
- ```ruby
36
- Consyncful.configure do |config|
37
- config.locale = 'en-NZ'
38
- config.contentful_client_options = {
39
- api_url: 'cdn.contentful.com',
40
- space: 'space_id',
41
- access_token: 'ACCESS TOKEN',
42
- environment: 'master', # optional
43
- logger: Logger.new(STDOUT) # optional for debugging
44
- }
45
- end
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
46
57
  ```
47
58
 
48
59
  ## Usage
@@ -129,7 +140,7 @@ instance.other_things.in_order # ordered the same as in Contentful
129
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:
130
141
 
131
142
  ```ruby
132
- Consyncful::Base.where(title: 'a title') # [ #<ModelName>, #<OtherModelName> ]
143
+ Consyncful::Base.where(title: 'a title') # [ #<ModelName>, #<OtherModelName> ]
133
144
  ```
134
145
 
135
146
  ### Sync callbacks
@@ -154,6 +165,21 @@ end
154
165
 
155
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)
156
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
+ ```
178
+
179
+ ### Why do I have to use MongoDB?
180
+
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.
182
+
157
183
  ## Development
158
184
 
159
185
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
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
@@ -8,10 +8,11 @@ module Consyncful
8
8
  include Mongoid::Document
9
9
  include Mongoid::Attributes::Dynamic
10
10
 
11
- store_in collection: 'contentful_models'
12
-
13
11
  cattr_accessor :model_map
14
12
 
13
+ store_in collection: -> { Consyncful.configuration.mongo_collection },
14
+ client: -> { Consyncful.configuration.mongo_client }
15
+
15
16
  def self.contentful_model_name(name)
16
17
  self.model_map ||= {}
17
18
 
@@ -22,7 +23,9 @@ module Consyncful
22
23
  def self.references_many(name)
23
24
  has_and_belongs_to_many name.to_sym, class_name: 'Consyncful::Base', inverse_of: nil do
24
25
  def in_order
25
- _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
26
29
  end
27
30
  end
28
31
  end
@@ -14,10 +14,9 @@ module Consyncful
14
14
  end
15
15
 
16
16
  def type
17
- if @item.type == 'Entry'
18
- @item.content_type.id
19
- elsif @item.type == 'Asset'
20
- 'asset'
17
+ case @item.type
18
+ when 'Entry' then @item.content_type.id
19
+ when 'Asset' then 'asset'
21
20
  end
22
21
  end
23
22
 
@@ -37,13 +36,11 @@ module Consyncful
37
36
  private
38
37
 
39
38
  def generic_fields
40
- fields = {}
41
- fields[:created_at] = @item.created_at
42
- fields[:updated_at] = @item.updated_at
43
- fields[:revision] = @item.revision
44
- fields[:contentful_type] = type
45
- fields[:synced_at] = Time.current
46
- 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 }
47
44
  end
48
45
 
49
46
  def localized_fields(default_locale)
@@ -52,9 +49,10 @@ module Consyncful
52
49
  @item.fields_with_locales.each do |field, value_with_locales|
53
50
  value_with_locales.each do |locale_code, value|
54
51
  next if value.is_a? Contentful::File # assets are handeled below
55
-
56
- fieldname = locale_code == default_locale.to_sym ? field : "#{field}_#{locale_code.to_s.underscore}".to_sym
57
- assign_field(fields, fieldname, value)
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
58
56
  end
59
57
  end
60
58
 
@@ -66,13 +64,20 @@ module Consyncful
66
64
  files_by_locale = @item.raw.dig('fields', 'file') || {}
67
65
 
68
66
  files_by_locale.each do |locale_code, details|
69
- fieldname = locale_code == default_locale ? 'file' : "file_#{locale_code.to_s.underscore}"
70
- fields[fieldname.to_sym] = details
67
+ field_name = localized_field_name('file', locale_code, default_locale)
68
+ fields[field_name.to_sym] = details
71
69
  end
72
70
 
73
71
  fields
74
72
  end
75
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
79
+ end
80
+
76
81
  def reference_value?(value)
77
82
  single_reference?(value) || many_reference?(value)
78
83
  end
@@ -85,14 +90,14 @@ module Consyncful
85
90
  value.is_a?(Array) && single_reference?(value.first)
86
91
  end
87
92
 
88
- def assign_field(hash, field, value)
93
+ def mapped_field_entry_for(field, value)
89
94
  if single_reference?(value)
90
- hash[ActiveSupport::Inflector.foreign_key(field).to_sym] = value.id
95
+ [ActiveSupport::Inflector.foreign_key(field).to_sym, value.id]
91
96
  elsif many_reference?(value)
92
97
  ids_field_name = field.to_s.singularize + '_ids' # fk field name
93
- hash[ids_field_name.to_sym] = value.map(&:id)
98
+ [ids_field_name.to_sym, value.map(&:id)]
94
99
  else
95
- hash[field] = value
100
+ [field, value]
96
101
  end
97
102
  end
98
103
  end
@@ -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'
@@ -19,6 +19,8 @@ module Consyncful
19
19
  include Mongoid::Document
20
20
  include Hooks
21
21
 
22
+ store_in client: -> { Consyncful.configuration.mongo_client }
23
+
22
24
  define_hook :before_run
23
25
  define_hook :after_run
24
26
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Consyncful
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consyncful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Anastasiadis-Gray
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2021-03-01 00:00:00.000000000 Z
13
+ date: 2021-04-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler