webflow_sync 2.0.0 → 3.1.2

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: 3fffb6d06856d3cd1a71f9180fff0753b3cf3396dcfeb1e5ed85d8a01d9ff05e
4
- data.tar.gz: a18bfeb55b4893dc0f9639d69259760e41d3326cda8096a471fc932534687844
3
+ metadata.gz: 0167ac7a16bf2611e4ba460c1365151bad0f4a502d2631d43452f18de030a799
4
+ data.tar.gz: 5bca098d13b884570d0188aeff52841daed1c4f9225c129f7b8a41091fb4e5e8
5
5
  SHA512:
6
- metadata.gz: d60c06119a661385dabfff3017fdfe72f3c515b0ba13c208486bff38b2cddc14f40bf5557fe691e5e66215d0f9fe75c7de0a8964e4ca6be725ef61fc3157e626
7
- data.tar.gz: 981e3169932af0aa8b7a528738d04da9e9a63cb0021c8c2bd6e3386e924384ee7cc3ae4f4840a02a4df95864d2b330755f83eea287d1752547d0124a1c7d192e
6
+ metadata.gz: de74a317b6b06e6b94ac2e4454cc32dd29cdca9ecdf1d22861c0d74760ce6a8639fff2bb80fe16a0dee4139b1000d5640ae46fcf7ed6ade9437c3bd0c90346dd
7
+ data.tar.gz: 58dcefd1fc8644d42db578a040f49fc1c89be2e24a06c5a7ca04ef2e85ddd65e6dc2fe245d32d4a7017ed66fadb8212b02544f430152ca5b36fd27377b389177
data/README.md CHANGED
@@ -5,7 +5,9 @@
5
5
 
6
6
  Keep your Ruby on Rails records in sync with WebFlow.*
7
7
 
8
- *Currently only one way Rails => WebFlow synchronization works
8
+ *Currently only one way Rails => WebFlow synchronization.
9
+
10
+ For the latest changes, check the [CHANGELOG.md](CHANGELOG.md).
9
11
 
10
12
  ## Installation
11
13
 
@@ -42,8 +44,9 @@ In `config/initializers/webflow_sync.rb` you can specify configuration options:
42
44
 
43
45
  1. `api_token`
44
46
  2. `webflow_site_id`
45
- 3. `skip_webflow_sync` - skip synchronization for different environments
46
- 4. `sync_webflow_slug` - save slug generated on WebFlow to the Rails database, in the Rails model column.
47
+ 3. `skip_webflow_sync` - skip synchronization for different environments.
48
+ 4. `publish_on_sync` - republish all domains after create, update and destroy actions.
49
+ 5. `sync_webflow_slug` - save slug generated on WebFlow to the Rails model, `webflow_slug` column.
47
50
 
48
51
  This can be useful if you want to link to WebFlow item directly from your Rails app:
49
52
 
@@ -56,7 +59,7 @@ In `config/initializers/webflow_sync.rb` you can specify configuration options:
56
59
  1. add `webflow_slug` column on the model table, then
57
60
  2. set the `sync_webflow_slug` option to `true`.
58
61
 
59
- Example:
62
+ Example:
60
63
 
61
64
  ```rb
62
65
  WebflowSync.configure do |config|
@@ -79,17 +82,27 @@ Please note that this _does not_ create a WebFlow collection as that's not possi
79
82
 
80
83
  As mentioned above, you need to create the WebFlow collection yourself.
81
84
 
82
- Make sure that the collection `slug` matches the Rails model collection name (the output of `model_class.model_name.collection`).
85
+ Make sure that the collection `slug` matches the Rails model collection name (the output of `model_class.model_name.to_s.underscore.dasherize.pluralize`).
83
86
 
84
87
  For example, for `Article` model:
85
88
 
86
89
  ```ruby
87
- > Article.model_name.collection
90
+ > Article.model_name.to_s.underscore.dasherize.pluralize
88
91
  # => "articles"
89
92
  ```
90
93
 
91
94
  Your WebFlow collection `slug` should be `"articles"`.
92
95
 
96
+ For Rails models named with multiple words, make a collection that have a space between words in the name. WebFlow will set the `slug` by replacing space for "-".
97
+
98
+ For example, for `FeaturedArticle` model:
99
+
100
+ ```ruby
101
+ > FeaturedArticle.model_name.to_s.underscore.dasherize.pluralize
102
+ # => "featured-articles"
103
+ ```
104
+ Your WebFlow collection `slug` in this case should be `"featured-articles"`.
105
+
93
106
  ### Set `webflow_site_id`
94
107
 
95
108
  There are couple of ways how you can set the `webflow_site_id` to be used.
@@ -156,13 +169,43 @@ Or if you already use `#as_json` for some other use-case and cannot modify it, y
156
169
  # app/models/article.rb
157
170
  class Article < ApplicationRecord
158
171
  include WebflowSync::ItemSync
159
-
172
+
160
173
  def as_webflow_json
161
174
  self.as_json
162
175
  end
163
176
  end
164
177
  ```
165
178
 
179
+ ### Sync a Rails model with the custom collection
180
+
181
+ If collection `slug` does not match the Rails model collection name, you can still sync with WebFlow collection, but need to specify collection slug for each `CreateItemJob` and `UpdateItemJob` call. If not specified, model name is used as a default slug name.
182
+ You would also need to replace included `WebflowSync::ItemSync` with custom callbacks that will call appropriate WebflowSync jobs.
183
+
184
+ For example:
185
+
186
+ ```ruby
187
+ WebflowSync::CreateItemJob.perform_later(model_name, id, collection_slug)
188
+ WebflowSync::UpdateItemJob.perform_later(model_name, id, collection_slug)
189
+ WebflowSync::DestroyItemJob.perform_later(collection_slug:, webflow_site_id:, webflow_item_id:)
190
+ ```
191
+
192
+ Where:
193
+
194
+ 1. `model_name` - Rails model that has `webflow_site_id` and `webflow_item_id` defined
195
+ 2. `collection_slug` - slug of the WebFlow collection (defaults to: `model_name.underscore.dasherize.pluralize `)
196
+
197
+
198
+ For example:
199
+
200
+ ```ruby
201
+ WebflowSync::CreateItemJob.perform_now('articles', 1, 'stories')
202
+ ```
203
+ Or, if you want to use the default 'articles' collection_slug:
204
+
205
+ ```ruby
206
+ WebflowSync::CreateItemJob.perform_now('articles', 1)
207
+ ```
208
+
166
209
  ### Run the initial sync
167
210
 
168
211
  After setting up which models you want to sync to WebFlow, you can run the initial sync for each of the models:
@@ -2,8 +2,16 @@
2
2
 
3
3
  module WebflowSync
4
4
  class CreateItemJob < ApplicationJob
5
- def perform(collection_slug, id)
6
- model_class = collection_slug.classify.constantize
5
+ # WebFlow collection slug should be in plural form.
6
+ # For collections that have spaces in their name, WebFlow sets slug by replacing space for "-".
7
+ # 'JobListing'.underscore.dasherize.pluralize => "job-listings"
8
+ # 'job_listing'.underscore.dasherize.pluralize => "job-listings"
9
+ # We can sync Rails model that has different class name than its WebFlow collection
10
+ # model_name => Rails model that has webflow_site_id and webflow_item_id columns
11
+ # collection_slug => slug of the WebFlow collection
12
+ # model_name = 'articles'; id = article.id, collection_slug = 'stories'
13
+ def perform(model_name, id, collection_slug = model_name.underscore.dasherize.pluralize)
14
+ model_class = model_name.underscore.classify.constantize
7
15
  record = model_class.find_by(id: id)
8
16
  return if record.blank?
9
17
  return if record.webflow_site_id.blank?
@@ -3,7 +3,7 @@
3
3
  module WebflowSync
4
4
  class InitialSyncJob < ApplicationJob
5
5
  def perform(collection_slug)
6
- model_class = collection_slug.classify.constantize
6
+ model_class = collection_slug.underscore.classify.constantize
7
7
  model_class.where(webflow_item_id: nil).find_each do |record|
8
8
  next if record.webflow_site_id.blank?
9
9
 
@@ -2,12 +2,16 @@
2
2
 
3
3
  module WebflowSync
4
4
  class UpdateItemJob < ApplicationJob
5
- def perform(collection_slug, id)
6
- model_class = collection_slug.classify.constantize
5
+ # WebFlow collection slug should be in plural form.
6
+ # For collections that have spaces in their name, WebFlow sets slug by replacing space for "-".
7
+ # 'JobListing'.underscore.dasherize.pluralize => 'job-listings'
8
+ # 'job_listing'.underscore.dasherize.pluralize => 'job-listings'
9
+ def perform(model_name, id, collection_slug = model_name.underscore.dasherize.pluralize)
10
+ model_class = model_name.underscore.classify.constantize
7
11
  record = model_class.find_by(id: id)
8
12
  return if record.blank?
9
13
  return if record.webflow_site_id.blank?
10
- return WebflowSync::CreateItemJob.perform_now(collection_slug, id) if record.webflow_item_id.blank?
14
+ return WebflowSync::CreateItemJob.perform_now(model_name, id, collection_slug) if record.webflow_item_id.blank?
11
15
 
12
16
  WebflowSync::Api.new(record.webflow_site_id).update_item(record, collection_slug)
13
17
  end
@@ -27,7 +27,10 @@ module WebflowSync
27
27
  def destroy_webflow_item
28
28
  return if self.skip_webflow_sync || WebflowSync.configuration.skip_webflow_sync
29
29
 
30
- WebflowSync::DestroyItemJob.perform_later(collection_slug: self.model_name.collection,
30
+ # Make sure slug is in the pulural form, and multiple words slug separated by dashes
31
+ collection_slug = self.model_name.collection.underscore.dasherize
32
+
33
+ WebflowSync::DestroyItemJob.perform_later(collection_slug: collection_slug,
31
34
  webflow_site_id: self.webflow_site_id,
32
35
  webflow_item_id: self.webflow_item_id)
33
36
  end
@@ -37,12 +37,16 @@ module WebflowSync
37
37
  make_request(:item, collection['_id'], webflow_item_id)
38
38
  end
39
39
 
40
- def create_item(record, collection_slug)
40
+ def create_item(record, collection_slug) # rubocop:disable Metrics/MethodLength
41
41
  collection = find_webflow_collection(collection_slug)
42
42
  response = make_request(:create_item, collection['_id'],
43
43
  record.as_webflow_json.reverse_merge(_archived: false, _draft: false), live: true)
44
44
 
45
45
  if update_record_colums(record, response)
46
+ # When the item is created, sometimes changes are not visible throughout the WebFlow site immediately (probably due to some caching).
47
+ # To make this change immediately visible from the WebFlow site, we need to publish the site.
48
+ publish
49
+
46
50
  puts "Created #{record.inspect} in #{collection_slug}"
47
51
  response
48
52
  else
@@ -56,6 +60,10 @@ module WebflowSync
56
60
  response = make_request(:update_item, { '_cid' => collection['_id'], '_id' => record.webflow_item_id },
57
61
  record.as_webflow_json.reverse_merge(_archived: false, _draft: false), live: true)
58
62
 
63
+ # When the item is updated, sometimes changes are not visible throughout the WebFlow site immediately (probably due to some caching).
64
+ # To make this change immediately visible from the WebFlow site, we need to publish the site.
65
+ publish
66
+
59
67
  puts "Updated #{record.inspect} in #{collection_slug}"
60
68
  response
61
69
  end
@@ -72,6 +80,8 @@ module WebflowSync
72
80
  end
73
81
 
74
82
  def publish
83
+ return unless WebflowSync.configuration.publish_on_sync
84
+
75
85
  response = make_request(:publish, site_id, domain_names: domain_names)
76
86
 
77
87
  puts "Publish all domains for Webflow site with id: #{site_id}"
@@ -5,4 +5,5 @@ WebflowSync.configure do |config|
5
5
  # config.skip_webflow_sync = Rails.env.test? # default
6
6
  config.webflow_site_id = ENV.fetch('WEBFLOW_SITE_ID')
7
7
  config.sync_webflow_slug = ENV.fetch('SYNC_WEBFLOW_SLUG')
8
+ config.publish_on_sync = true
8
9
  end
@@ -6,10 +6,13 @@ module WebflowSync
6
6
 
7
7
  def configure
8
8
  self.configuration ||= Configuration.new
9
+
10
+ self.configuration.publish_on_sync = true
11
+ self.configuration.skip_webflow_sync = !Rails.env.production?
12
+
9
13
  yield(self.configuration)
10
14
 
11
15
  self.configuration.api_token ||= ENV.fetch('WEBFLOW_API_TOKEN')
12
- defined?(self.configuration.skip_webflow_sync) or self.configuration.skip_webflow_sync = !Rails.env.production?
13
16
  end
14
17
 
15
18
  private
@@ -18,7 +21,7 @@ module WebflowSync
18
21
  end
19
22
 
20
23
  class Configuration
21
- attr_accessor :skip_webflow_sync, :webflow_site_id, :sync_webflow_slug
24
+ attr_accessor :skip_webflow_sync, :webflow_site_id, :sync_webflow_slug, :publish_on_sync
22
25
  attr_reader :api_token
23
26
 
24
27
  def api_token=(value)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebflowSync
4
- VERSION = '2.0.0'
4
+ VERSION = '3.1.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webflow_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viktor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-29 00:00:00.000000000 Z
11
+ date: 2021-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dotenv-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: factory_bot_rails
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -173,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
187
  - !ruby/object:Gem::Version
174
188
  version: '0'
175
189
  requirements: []
176
- rubygems_version: 3.0.9
190
+ rubygems_version: 3.2.28
177
191
  signing_key:
178
192
  specification_version: 4
179
193
  summary: Keep Rails models in sync with WebFlow.