Refresh products cache when coordinator fees are changed or destroyed

This commit is contained in:
Rohan Mitchell
2016-02-04 12:45:05 +11:00
parent 540687515e
commit 0a90a48b04
3 changed files with 34 additions and 5 deletions

View File

@@ -1,4 +1,15 @@
class CoordinatorFee < ActiveRecord::Base
belongs_to :order_cycle
belongs_to :enterprise_fee
after_save :refresh_products_cache
after_destroy :refresh_products_cache
private
def refresh_products_cache
order_cycle.refresh_products_cache
end
end

View File

@@ -241,6 +241,10 @@ class OrderCycle < ActiveRecord::Base
coordinator.users.include? user
end
def refresh_products_cache
OpenFoodNetwork::ProductsCache.order_cycle_changed self
end
private
@@ -254,9 +258,4 @@ class OrderCycle < ActiveRecord::Base
distributed_variants.include?(product.master) &&
(product.variants & distributed_variants).empty?
end
def refresh_products_cache
OpenFoodNetwork::ProductsCache.order_cycle_changed self
end
end

View File

@@ -0,0 +1,19 @@
require 'spec_helper'
describe CoordinatorFee do
describe "products caching" do
let(:order_cycle) { create(:simple_order_cycle) }
let(:enterprise_fee) { create(:enterprise_fee) }
it "refreshes the products cache on change" do
expect(OpenFoodNetwork::ProductsCache).to receive(:order_cycle_changed).with(order_cycle)
order_cycle.coordinator_fees << enterprise_fee
end
it "refreshes the products cache on destruction" do
order_cycle.coordinator_fees << enterprise_fee
expect(OpenFoodNetwork::ProductsCache).to receive(:order_cycle_changed).with(order_cycle)
order_cycle.coordinator_fee_refs.first.destroy
end
end
end