Refresh cache when option value changed or destroyed

This commit is contained in:
Rohan Mitchell
2016-02-25 11:08:53 +11:00
parent 71862e00a7
commit b5204a4820
2 changed files with 46 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
module Spree
OptionValue.class_eval do
after_save :refresh_products_cache
around_destroy :refresh_products_cache_from_destroy
private
def refresh_products_cache
variants(:reload).each &:refresh_products_cache
end
def refresh_products_cache_from_destroy
vs = variants(:reload).to_a
yield
vs.each &:refresh_products_cache
end
end
end

View File

@@ -0,0 +1,26 @@
require 'spec_helper'
module Spree
describe OptionValue do
describe "products cache" do
let(:variant) { create(:variant) }
let(:option_value) { create(:option_value) }
before do
variant.option_values << option_value
option_value.reload
end
it "refreshes the products cache on change, via variant" do
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant)
option_value.name = 'foo'
option_value.save!
end
it "refreshes the products cache on destruction, via variant" do
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant)
option_value.destroy
end
end
end
end