diff --git a/app/models/spree/option_type_decorator.rb b/app/models/spree/option_type_decorator.rb new file mode 100644 index 0000000000..aea706c847 --- /dev/null +++ b/app/models/spree/option_type_decorator.rb @@ -0,0 +1,13 @@ +module Spree + OptionType.class_eval do + has_many :products, through: :product_option_types + after_save :refresh_products_cache + + + private + + def refresh_products_cache + products(:reload).each &:refresh_products_cache + end + end +end diff --git a/spec/models/spree/option_type_spec.rb b/spec/models/spree/option_type_spec.rb new file mode 100644 index 0000000000..e93f35ec49 --- /dev/null +++ b/spec/models/spree/option_type_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +module Spree + describe OptionType do + describe "products cache" do + let!(:product) { create(:simple_product, option_types: [option_type]) } + let(:option_type) { create(:option_type) } + + before { option_type.reload } + + it "refreshes the products cache on change, via product" do + expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product) + option_type.name = 'foo' + option_type.save! + end + + # When a OptionType is destroyed, the destruction of the OptionValues refreshes the cache + end + end +end