Perform refresh of products cache on product change

This commit is contained in:
Rohan Mitchell
2016-01-28 16:00:55 +11:00
parent 0c0c98a0b0
commit c98e44c5a1
2 changed files with 26 additions and 4 deletions

View File

@@ -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)

View File

@@ -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) }