mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Perform refresh of products cache on product change
This commit is contained in:
@@ -4,14 +4,14 @@ module OpenFoodNetwork
|
||||
# the products cache.
|
||||
class ProductsCache
|
||||
def self.variant_changed(variant)
|
||||
exchanges_featuring_variant(variant).each do |exchange|
|
||||
exchanges_featuring_variants(variant).each do |exchange|
|
||||
refresh_cache exchange.receiver, exchange.order_cycle
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def self.variant_destroyed(variant, &block)
|
||||
exchanges = exchanges_featuring_variant(variant).to_a
|
||||
exchanges = exchanges_featuring_variants(variant).to_a
|
||||
|
||||
block.call
|
||||
|
||||
@@ -22,15 +22,18 @@ module OpenFoodNetwork
|
||||
|
||||
|
||||
def self.product_changed(product)
|
||||
exchanges_featuring_variants(product.variants).each do |exchange|
|
||||
refresh_cache exchange.receiver, exchange.order_cycle
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def self.exchanges_featuring_variant(variant)
|
||||
def self.exchanges_featuring_variants(variants)
|
||||
Exchange.
|
||||
outgoing.
|
||||
with_variant(variant).
|
||||
with_any_variant(variants).
|
||||
joins(:order_cycle).
|
||||
merge(OrderCycle.dated).
|
||||
merge(OrderCycle.not_closed)
|
||||
|
||||
@@ -70,6 +70,25 @@ module OpenFoodNetwork
|
||||
end
|
||||
end
|
||||
|
||||
describe "when a product changes" do
|
||||
let(:product) { create(:simple_product) }
|
||||
let(:v1) { create(:variant, product: product) }
|
||||
let(:v2) { create(:variant, product: product) }
|
||||
let(:d1) { create(:distributor_enterprise) }
|
||||
let(:d2) { create(:distributor_enterprise) }
|
||||
let(:oc) { create(:open_order_cycle) }
|
||||
let!(:ex1) { create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d1, variants: [v1]) }
|
||||
let!(:ex2) { create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d2, variants: [v1, v2]) }
|
||||
|
||||
before { product.reload }
|
||||
|
||||
it "refreshes the distribution each variant appears in, once each" do
|
||||
expect(ProductsCache).to receive(:refresh_cache).with(d1, oc).once
|
||||
expect(ProductsCache).to receive(:refresh_cache).with(d2, oc).once
|
||||
ProductsCache.product_changed product
|
||||
end
|
||||
end
|
||||
|
||||
describe "refreshing the cache" do
|
||||
let(:distributor) { double(:distributor, id: 123) }
|
||||
let(:order_cycle) { double(:order_cycle, id: 456) }
|
||||
|
||||
Reference in New Issue
Block a user