webflow_sync 0.2.0 → 0.3.0

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: 36b6c5dc80e752a95692afe908be134cdefd6193e0ed712c7a6d41440bb679e0
4
- data.tar.gz: 5bd56794f4d0f09634a776b18ca613844b149afaf99cbbb841fe6294ae5cc67a
3
+ metadata.gz: 462ca934ced66c135d9737e6d3a488d93e26c90b4a3e48e1e0b5b8a87c710fb4
4
+ data.tar.gz: 9473b40c6a4bc830a864902c013de92936edfd3b81985a77353c90dae481b905
5
5
  SHA512:
6
- metadata.gz: 83fe81d28e2aec28ef31bee07956f8a105b368053f60dbcff1e668ae853671e708e543db1444aad37571117562ac7c6412913600c1da0d35d80851d5438a827c
7
- data.tar.gz: 9e40ae99fa67b4d6ae787900e288009f2d7bc3c6633908e36f13efec62df3b676ec23ddcf0710cbd82a80beb5a573bf13da69c2d13c8f49c9db57686f8d60428
6
+ metadata.gz: 74d85eea27d5d66ddc36ae45c336422f64dc447a83d82a0764abcdcb470f6148732d60b76ac2209b6d9d37bab07e5fc28281c961f2e6d9bdf380dd5c8c6bc15e
7
+ data.tar.gz: ebec0cbb65c0f521724366c3fe85be6b0f4c4e17939d62c5f3a0ffc3639a0003161e1d3c05676bf72af9a7e55816e03aa9e9f4c3afe81c1c4bc1149a03144a4b
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- ![build](https://github.com/vfonic/webflow_sync/workflows/build/badge.svg)
1
+ [![Build Status](https://github.com/vfonic/webflow_sync/workflows/build/badge.svg)](https://github.com/vfonic/webflow_sync/actions)
2
+
2
3
 
3
4
  # WebflowSync
4
5
 
@@ -158,6 +159,11 @@ This gem silently "fails" (does nothing) when `webflow_site_id` or `webflow_item
158
159
 
159
160
  PRs welcome!
160
161
 
162
+ ## Thanks and Credits
163
+
164
+ This gem wouldn't be possible without the amazing work of [webflow-ruby](https://github.com/penseo/webflow-ruby) gem. Thank you, @phoet!
165
+
166
+
161
167
  ## License
162
168
 
163
169
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -7,8 +7,18 @@ module WebflowSync
7
7
  model_class.where(webflow_item_id: nil).find_each do |record|
8
8
  next if record.webflow_site_id.blank?
9
9
 
10
- WebflowSync::Api.new(record.webflow_site_id).create_item(record, collection_slug)
10
+ client(record.webflow_site_id).create_item(record, collection_slug)
11
11
  end
12
12
  end
13
+
14
+ private
15
+
16
+ def client(site_id)
17
+ if @client&.site_id == site_id
18
+ @client
19
+ else
20
+ @client = WebflowSync::Api.new(site_id)
21
+ end
22
+ end
13
23
  end
14
24
  end
@@ -7,7 +7,7 @@ module WebflowSync
7
7
  record = model_class.find_by(id: id)
8
8
  return if record.blank?
9
9
  return if record.webflow_site_id.blank?
10
- return if record.webflow_item_id.blank?
10
+ return WebflowSync::CreateItemJob.perform_now(collection_slug, id) if record.webflow_item_id.blank?
11
11
 
12
12
  WebflowSync::Api.new(record.webflow_site_id).update_item(record, collection_slug)
13
13
  end
@@ -8,9 +8,9 @@ module WebflowSync
8
8
  included do
9
9
  attr_accessor :skip_webflow_sync
10
10
 
11
- after_create :create_webflow_item
12
- after_update :update_webflow_item
13
- after_destroy :destroy_webflow_item
11
+ after_commit :create_webflow_item, on: :create
12
+ after_commit :update_webflow_item, on: :update
13
+ after_commit :destroy_webflow_item, on: :destroy
14
14
 
15
15
  def create_webflow_item
16
16
  return if self.skip_webflow_sync || WebflowSync.configuration.skip_webflow_sync
@@ -8,16 +8,21 @@ module WebflowSync
8
8
  @site_id = site_id
9
9
  end
10
10
 
11
- def create_item(record, collection_slug)
11
+ def create_item(record, collection_slug) # rubocop:disable Metrics/MethodLength
12
12
  collection = find_webflow_collection(collection_slug)
13
13
  response = client.create_item(
14
14
  collection['_id'],
15
15
  record.as_webflow_json.reverse_merge(_archived: false, _draft: false), live: true
16
16
  )
17
17
 
18
- record.update!(webflow_item_id: response['_id'])
19
- puts "Created #{record.inspect} in #{collection_slug}"
20
- response
18
+ # use update_column to skip callbacks to prevent WebflowSync::ItemSync to kick off
19
+ if record.update_column(:webflow_item_id, response['_id']) # rubocop:disable Rails/SkipsModelValidations
20
+ puts "Created #{record.inspect} in #{collection_slug}"
21
+ response
22
+ else
23
+ raise "Failed to store webflow_item_id: '#{response['_id']}' " \
24
+ "after creating item in WebFlow collection #{record.inspect}"
25
+ end
21
26
  end
22
27
 
23
28
  def update_item(record, collection_slug)
@@ -9,7 +9,7 @@ module WebflowSync
9
9
  yield(self.configuration)
10
10
 
11
11
  self.configuration.api_token ||= ENV.fetch('WEBFLOW_API_TOKEN')
12
- self.configuration.skip_webflow_sync ||= Rails.env.test?
12
+ defined?(self.configuration.skip_webflow_sync) or self.configuration.skip_webflow_sync = !Rails.env.production?
13
13
  end
14
14
 
15
15
  private
@@ -18,7 +18,8 @@ module WebflowSync
18
18
  end
19
19
 
20
20
  class Configuration
21
- attr_accessor :api_token, :skip_webflow_sync, :webflow_site_id
21
+ attr_accessor :skip_webflow_sync, :webflow_site_id
22
+ attr_reader :api_token
22
23
 
23
24
  def api_token=(value)
24
25
  @api_token = Webflow.config.api_token = value
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebflowSync
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
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: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viktor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-01 00:00:00.000000000 Z
11
+ date: 2021-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails