Compare commits

...

6 Commits

Author SHA1 Message Date
Pau Pérez Fabregat
0f2df6a941 Merge pull request #3891 from openfoodfoundation/2-0-1
Bring changes introduced in v2.0.1 into 2-0-stable
2019-06-03 22:01:22 +02:00
Luis Ramos
278147b81f Merge pull request #3892 from coopdevs/hide-product-from-shopfront-when-out-of-stock
Hide product from shopfront when out of stock
2019-05-31 19:21:47 +01:00
Pau Perez
7ea10f6d98 Refresh the products cache on stock change
This ensures the cache is up-to-date when a user accesses the shopfront
and we use the appropriate stock levels.

Besides, this makes the (very annoying) ProductsCacheIntegrityCheckerJob
succeed and thus Bugsnag won't be flooded with failures anymore.

As for the implementation of this. I'm not happy at all. It's disgusting
to use `class_eval` for this but it's a good trade-off to deliver this
fix ASAP. Katuma's been experiencing this error far too long
🙈. I was working on a better implementation that required
a bit of refactoring but it was taking too much time. I'll open another
PR with it soon.
2019-05-31 10:35:05 +02:00
Pau Perez
545c7c99ed Move spec to its spec/ folder 2019-05-31 09:30:26 +02:00
Pau Pérez Fabregat
cc3a0877c4 Merge pull request #3849 from luisramos0/pr_3839
Get PR 3839 to 2-0-1
2019-05-13 15:27:04 +02:00
Pau Perez
bb12592a74 Report cache diff to Bugsnag in a new tab
This will allow us to see the difference between the cache entry and the
actual shopfront. Otherwise, there is no way to see what wasn't
refreshed in the cache.
2019-05-10 22:02:11 +01:00
5 changed files with 29 additions and 3 deletions

View File

@@ -3,7 +3,14 @@ require 'open_food_network/products_cache_integrity_checker'
ProductsCacheIntegrityCheckerJob = Struct.new(:distributor_id, :order_cycle_id) do
def perform
unless checker.ok?
Bugsnag.notify RuntimeError.new("Products JSON differs from cached version for distributor: #{distributor_id}, order cycle: #{order_cycle_id}"), diff: checker.diff.to_s(:text)
exception = RuntimeError.new(
"Products JSON differs from cached version for distributor: #{distributor_id}, " \
"order cycle: #{order_cycle_id}"
)
Bugsnag.notify(exception) do |report|
report.add_tab(:products_cache, diff: checker.diff.to_s(:text))
end
end
end

View File

@@ -0,0 +1,7 @@
Spree::StockItem.class_eval do
after_save :refresh_products_cache
def refresh_products_cache
OpenFoodNetwork::ProductsCache.variant_changed(variant)
end
end

View File

@@ -0,0 +1,10 @@
require 'spec_helper'
describe Spree::StockItem do
let!(:variant) { create(:variant) }
it 'refreshes the products cache on save' do
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant)
variant.on_hand = -2
end
end

View File

@@ -166,7 +166,8 @@ module Spree
let(:variant) { create(:variant) }
it "refreshes the products cache on save" do
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant)
# When creating the variant both the Variant and StockItem callbacks get executed
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant).twice
variant.sku = 'abc123'
variant.save
end
@@ -182,7 +183,8 @@ module Spree
it "refreshes the products cache for the entire product on save" do
expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product)
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).never
# The StockItem callback is still executed
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).once
master.sku = 'abc123'
master.save
end