jekyll-spree-client 0.1.16 → 0.1.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jekyll/spree_client.rb +32 -12
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4fae6ac8495376030f27da4d790f5935e0f20c2ada484eced1a266c98704ec82
4
- data.tar.gz: 144cbab5df09cab4c48c1a0d2c16ee2f0a9c593e273750af346d143f9526ad24
3
+ metadata.gz: cb1e9f91b6b5df295223e22a1a2323ddae55d62df040577a780b58d48ba5138f
4
+ data.tar.gz: 5d470993e67857cc5dbbba06c55dc29ae685c557b07b524a86899f6de7fe69dc
5
5
  SHA512:
6
- metadata.gz: ede792ab10edef6455c9a32f7eb42f5ad6dadc4e4c9ab59ebf06c141073177aec0264a6a236da51099108f66e1893ae1c9e5a51b36aeb97efc7357a92ec80176
7
- data.tar.gz: 40b3025ef2c89824713d338cc9d9151a7c765803c90e3c16c3bf8430304e62cf63a5d7a71a547f3705786e3ac5a65808c5ba88adc01a0a0c48a40f799f12e9c1
6
+ metadata.gz: 906fe73618cfaebffd6d61cf49d74dd031ed22df64710bbc6c0e9fb51feeef9fd651457c40543f78170e5a8a8843eaebd22c010fdce7cf804cec0375a473b86b
7
+ data.tar.gz: 5ffb592af45112839f81145fa60720d0a27583690b65e3b6d7a6cb4bfbdec4fbec1dca23e832e633b31e019b53548439ac8b69814112c9f23f62beb4ff7822e8
@@ -33,21 +33,26 @@ module Jekyll
33
33
  #
34
34
  # @return [Array]
35
35
  def variant_fields
36
- @variant_fields ||= %w[price weight height width depth cost_price pay_what_you_can].freeze
36
+ @variant_fields ||= %w[price weight height width depth cost_price].freeze
37
37
  end
38
38
 
39
39
  # Fields that can change locally and we need to sync to Spree
40
40
  #
41
41
  # @return [Array]
42
42
  def product_fields
43
- @product_fields ||= %w[name description meta_description meta_keywords meta_title].freeze
43
+ @product_fields ||= %w[name description meta_description meta_keywords meta_title pay_what_you_can].freeze
44
44
  end
45
45
 
46
46
  # All fields, using during creation
47
47
  #
48
48
  # @return [Array]
49
49
  def create_fields
50
- @create_fields ||= product_fields + variant_fields
50
+ @create_fields ||= (product_fields + variant_fields).freeze
51
+ end
52
+
53
+ # Fields which are only updated on the Variant.
54
+ def variant_only_fields
55
+ @variant_only_fields ||= %w[track_inventory].freeze
51
56
  end
52
57
 
53
58
  # Spree Client
@@ -180,18 +185,23 @@ module Jekyll
180
185
  # @return [true,false]
181
186
  def update(product, variant)
182
187
  # Sync local changes to Spree
183
- remote_product = product.data.slice(*product_fields).transform_keys(&:to_sym)
184
- variant_comparison = variant.slice(*product_fields).transform_keys(&:to_sym)
188
+ remote_product = attributes_from product, product_fields
189
+ variant_comparison = attributes_from variant, product_fields
185
190
 
186
191
  # Only when they haven't changed, so we don't waste time sending a
187
192
  # request.
188
193
  unless remote_product.slice(*variant_comparison.keys) == variant_comparison
189
194
  # XXX: We don't have the product ID with the variant but we have
190
195
  # the slug.
196
+ remote_variant = { id: variant['id'] }.merge!(attributes_from product, variant_only_fields)
197
+
198
+ remote_product.delete_if do |k, v|
199
+ remote_variant.key? k
200
+ end
201
+
191
202
  remote_product[:id] = variant['slug']
192
- remote_product[:sku] = product.data[sku_field]
193
203
 
194
- unless remote_products.update **remote_product
204
+ unless remote_products.update(**remote_product) && spree.variants.update(**remote_variant)
195
205
  Jekyll.logger.error "Couldn't update #{product.data['title']}"
196
206
  mark_with_error product, error_messages
197
207
  end
@@ -218,21 +228,22 @@ module Jekyll
218
228
  # @param [Jekyll::Document]
219
229
  # @return [true,false]
220
230
  def create(product)
221
- new_product = product.data.slice(*create_fields).transform_keys(&:to_sym)
222
- new_product[:sku] = product.data[sku_field]
231
+ new_product = attributes_from(product, create_fields)
223
232
 
224
- unless remote_products.create **new_product
233
+ unless remote_products.create(**new_product)
225
234
  Jekyll.logger.error "Couldn't create #{product.data['title']}"
226
235
  mark_with_error product, error_messages
227
236
 
228
237
  return false
229
238
  end
230
239
 
231
- product.data['variant_id'] = remote_products.response.dig('master', 'id')
240
+ variant_id = remote_products.response.dig('master', 'id')
241
+ spree.variants(id: variant_id).update(attributes_from(product, variant_only_fields))
242
+ product.data['variant_id'] = variant_id
232
243
  product.data['in_stock'] = product.data['stock'].to_i > 0
233
244
 
234
245
  # Add the initial stock
235
- spree.stock_items(id: product.data['variant_id'], stock_location_id: stock_location['id'])
246
+ spree.stock_items(id: variant_id, stock_location_id: stock_location['id'])
236
247
  .stock_movements.create(quantity: product.data['stock'])
237
248
 
238
249
  true
@@ -246,5 +257,14 @@ module Jekyll
246
257
  end
247
258
  end&.flatten
248
259
  end
260
+
261
+ # Converts fields from a Jekyll::Document into an attribute Hash.
262
+ #
263
+ # @param [Jekyll::Document,Hash] Document or Hash to extract data from
264
+ # @param [Array] An Array of field names
265
+ # @return [Hash] A symbolized Hash of fields and values
266
+ def attributes_from(document_or_hash, fields)
267
+ (document_or_hash.respond_to?(:data) ? document_or_hash.data : document_or_hash).slice(*fields).transform_keys(&:to_sym)
268
+ end
249
269
  end
250
270
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-spree-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.16
4
+ version: 0.1.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-29 00:00:00.000000000 Z
11
+ date: 2021-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spree-api-client