diff --git a/app/models/spree/option_value_decorator.rb b/app/models/spree/option_value_decorator.rb new file mode 100644 index 0000000000..cfe9c23cca --- /dev/null +++ b/app/models/spree/option_value_decorator.rb @@ -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 diff --git a/spec/models/spree/option_value_spec.rb b/spec/models/spree/option_value_spec.rb new file mode 100644 index 0000000000..f784e08af2 --- /dev/null +++ b/spec/models/spree/option_value_spec.rb @@ -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