mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Refresh products cache when inventory items are changed
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
require 'open_food_network/products_cache'
|
||||
|
||||
class InventoryItem < ActiveRecord::Base
|
||||
attr_accessible :enterprise, :enterprise_id, :variant, :variant_id, :visible
|
||||
|
||||
@@ -11,4 +13,13 @@ class InventoryItem < ActiveRecord::Base
|
||||
|
||||
scope :visible, where(visible: true)
|
||||
scope :hidden, where(visible: false)
|
||||
|
||||
after_save :refresh_products_cache
|
||||
|
||||
|
||||
private
|
||||
|
||||
def refresh_products_cache
|
||||
OpenFoodNetwork::ProductsCache.inventory_item_changed self
|
||||
end
|
||||
end
|
||||
|
||||
@@ -95,6 +95,13 @@ module OpenFoodNetwork
|
||||
end
|
||||
|
||||
|
||||
def self.inventory_item_changed(inventory_item)
|
||||
exchanges_featuring_variants(inventory_item.variant, distributor: inventory_item.enterprise).each do |exchange|
|
||||
refresh_cache exchange.receiver, exchange.order_cycle
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def self.exchanges_featuring_variants(variants, distributor: nil)
|
||||
|
||||
@@ -386,6 +386,25 @@ module OpenFoodNetwork
|
||||
end
|
||||
end
|
||||
|
||||
describe "when an inventory item is changed" do
|
||||
let!(:d) { create(:distributor_enterprise) }
|
||||
let!(:v) { create(:variant) }
|
||||
let!(:oc1) { create(:open_order_cycle, distributors: [d], variants: [v]) }
|
||||
let(:oc2) { create(:open_order_cycle, distributors: [d], variants: []) }
|
||||
let!(:ii) { create(:inventory_item, enterprise: d, variant: v) }
|
||||
|
||||
it "updates each distribution for that enterprise+variant" do
|
||||
expect(ProductsCache).to receive(:refresh_cache).with(d, oc1)
|
||||
ProductsCache.inventory_item_changed ii
|
||||
end
|
||||
|
||||
it "doesn't update distributions that don't feature the variant" do
|
||||
oc2
|
||||
expect(ProductsCache).to receive(:refresh_cache).with(d, oc2).never
|
||||
ProductsCache.inventory_item_changed ii
|
||||
end
|
||||
end
|
||||
|
||||
describe "refreshing the cache" do
|
||||
let(:distributor) { double(:distributor) }
|
||||
let(:order_cycle) { double(:order_cycle) }
|
||||
|
||||
16
spec/models/inventory_item_spec.rb
Normal file
16
spec/models/inventory_item_spec.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
require 'spec_helper'
|
||||
require 'open_food_network/products_cache'
|
||||
|
||||
describe InventoryItem do
|
||||
describe "caching" do
|
||||
let(:ii) { create(:inventory_item) }
|
||||
|
||||
it "refreshes the products cache on save" do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:inventory_item_changed).with(ii)
|
||||
ii.visible = false
|
||||
ii.save
|
||||
end
|
||||
|
||||
# Inventory items are not destroyed
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user