From 8b070fddbbd79bc08ad712dfc1a815831e392d4c Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 5 Feb 2016 10:40:27 +1100 Subject: [PATCH] Refresh products cache when exchange fee is changed or destroyed --- app/models/exchange_fee.rb | 11 +++++++++++ spec/models/exchange_fee_spec.rb | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 spec/models/exchange_fee_spec.rb diff --git a/app/models/exchange_fee.rb b/app/models/exchange_fee.rb index ff9f12e8dd..03fce8ce04 100644 --- a/app/models/exchange_fee.rb +++ b/app/models/exchange_fee.rb @@ -1,4 +1,15 @@ class ExchangeFee < ActiveRecord::Base belongs_to :exchange belongs_to :enterprise_fee + + + after_save :refresh_products_cache + after_destroy :refresh_products_cache + + + private + + def refresh_products_cache + exchange.refresh_products_cache + end end diff --git a/spec/models/exchange_fee_spec.rb b/spec/models/exchange_fee_spec.rb new file mode 100644 index 0000000000..12bcd0c5e3 --- /dev/null +++ b/spec/models/exchange_fee_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +describe ExchangeFee do + describe "products caching" do + let(:exchange) { create(:exchange) } + let(:enterprise_fee) { create(:enterprise_fee) } + + it "refreshes the products cache on change" do + expect(OpenFoodNetwork::ProductsCache).to receive(:exchange_changed).with(exchange) + exchange.enterprise_fees << enterprise_fee + end + + it "refreshes the products cache on destruction" do + exchange.enterprise_fees << enterprise_fee + expect(OpenFoodNetwork::ProductsCache).to receive(:exchange_changed).with(exchange) + exchange.reload.exchange_fees.destroy_all + end + end +end