diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index e969dd500b..2c51ec5420 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -191,6 +191,11 @@ Spree::Product.class_eval do alias_method_chain :delete, :delete_from_order_cycles + def refresh_products_cache + OpenFoodNetwork::ProductsCache.product_changed self + end + + private def set_available_on_to_now @@ -251,8 +256,4 @@ Spree::Product.class_eval do self.permalink = create_unique_permalink(requested.parameterize) end end - - def refresh_products_cache - OpenFoodNetwork::ProductsCache.product_changed self - end end diff --git a/app/models/spree/product_property_decorator.rb b/app/models/spree/product_property_decorator.rb new file mode 100644 index 0000000000..0f3329c03d --- /dev/null +++ b/app/models/spree/product_property_decorator.rb @@ -0,0 +1,10 @@ +module Spree + ProductProperty.class_eval do + after_save :refresh_products_cache + after_destroy :refresh_products_cache + + def refresh_products_cache + product.refresh_products_cache + end + end +end diff --git a/app/models/spree/property_decorator.rb b/app/models/spree/property_decorator.rb new file mode 100644 index 0000000000..5b8e53338c --- /dev/null +++ b/app/models/spree/property_decorator.rb @@ -0,0 +1,15 @@ +module Spree + Property.class_eval do + after_save :refresh_products_cache + + # When a Property is destroyed, dependent-destroy will destroy all ProductProperties, + # which will take care of refreshing the products cache + + + private + + def refresh_products_cache + product_properties(:reload).each &:refresh_products_cache + end + end +end diff --git a/spec/models/spree/product_property_spec.rb b/spec/models/spree/product_property_spec.rb new file mode 100644 index 0000000000..1c9799ab29 --- /dev/null +++ b/spec/models/spree/product_property_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +module Spree + describe ProductProperty do + describe "callbacks" do + let(:product) { product_property.product } + let(:product_property) { create(:product_property) } + + it "refreshes the products cache on save, via Product" do + expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product) + product_property.value = 123 + product_property.save + end + + it "refreshes the products cache on destroy" do + expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product) + product_property.destroy + end + end + end +end diff --git a/spec/models/spree/property_spec.rb b/spec/models/spree/property_spec.rb new file mode 100644 index 0000000000..a86513b5a9 --- /dev/null +++ b/spec/models/spree/property_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +module Spree + describe Property do + describe "callbacks" do + let(:property) { product_property.property } + let(:product) { product_property.product } + let(:product_property) { create(:product_property) } + + it "refreshes the products cache on save" do + expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product) + property.name = 'asdf' + property.save + end + end + end +end