From 7ea10f6d9878554a709ba51df57d190ba3bb53ff Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 31 May 2019 09:48:39 +0200 Subject: [PATCH] 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 :see_no_evil:. 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. --- app/models/spree/stock_item_decorator.rb | 7 +++++++ spec/models/spree/stock_item_spec.rb | 10 ++++++++++ spec/models/spree/variant_spec.rb | 6 ++++-- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 app/models/spree/stock_item_decorator.rb create mode 100644 spec/models/spree/stock_item_spec.rb diff --git a/app/models/spree/stock_item_decorator.rb b/app/models/spree/stock_item_decorator.rb new file mode 100644 index 0000000000..e115225c4f --- /dev/null +++ b/app/models/spree/stock_item_decorator.rb @@ -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 diff --git a/spec/models/spree/stock_item_spec.rb b/spec/models/spree/stock_item_spec.rb new file mode 100644 index 0000000000..f934084784 --- /dev/null +++ b/spec/models/spree/stock_item_spec.rb @@ -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 diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index b5643e0109..1cf9228b52 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -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