Perform refresh of products cache for variant override change

This commit is contained in:
Rohan Mitchell
2016-01-29 10:14:15 +11:00
parent 5f188650d8
commit b7a88fd03b
2 changed files with 28 additions and 2 deletions

View File

@@ -29,6 +29,9 @@ module OpenFoodNetwork
def self.variant_override_changed(variant_override)
exchanges_featuring_variants(variant_override.variant, distributor: variant_override.hub).each do |exchange|
refresh_cache exchange.receiver, exchange.order_cycle
end
end
@@ -38,13 +41,17 @@ module OpenFoodNetwork
private
def self.exchanges_featuring_variants(variants)
Exchange.
def self.exchanges_featuring_variants(variants, distributor: nil)
exchanges = Exchange.
outgoing.
with_any_variant(variants).
joins(:order_cycle).
merge(OrderCycle.dated).
merge(OrderCycle.not_closed)
exchanges = exchanges.to_enterprise(distributor) if distributor
exchanges
end

View File

@@ -89,6 +89,25 @@ module OpenFoodNetwork
end
end
describe "when a variant override changes" do
let(:variant) { create(:variant) }
let(:d1) { create(:distributor_enterprise) }
let(:d2) { create(:distributor_enterprise) }
let!(:vo) { create(:variant_override, variant: variant, hub: d1) }
let!(:oc) { create(:open_order_cycle, distributors: [d1, d2], variants: [variant]) }
it "refreshes the distributions that the variant override affects" do
expect(ProductsCache).to receive(:refresh_cache).with(d1, oc).once
ProductsCache.variant_override_changed vo
end
it "does not refresh other distributors of the variant" do
expect(ProductsCache).to receive(:refresh_cache).with(d2, oc).never
ProductsCache.variant_override_changed vo
end
end
describe "refreshing the cache" do
let(:distributor) { double(:distributor, id: 123) }
let(:order_cycle) { double(:order_cycle, id: 456) }