diff --git a/app/models/spree/price_decorator.rb b/app/models/spree/price_decorator.rb new file mode 100644 index 0000000000..340a6aa17a --- /dev/null +++ b/app/models/spree/price_decorator.rb @@ -0,0 +1,13 @@ +module Spree + Price.class_eval do + after_save :refresh_products_cache + after_destroy :refresh_products_cache + + + private + + def refresh_products_cache + variant.refresh_products_cache + end + end +end diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 9efa89795e..7be9d712d0 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -85,12 +85,6 @@ Spree::Variant.class_eval do end end - private - - def update_weight_from_unit_value - self.weight = weight_from_unit_value if self.product.variant_unit == 'weight' && unit_value.present? - end - def refresh_products_cache if is_master? product.refresh_products_cache @@ -99,6 +93,13 @@ Spree::Variant.class_eval do end end + + private + + def update_weight_from_unit_value + self.weight = weight_from_unit_value if self.product.variant_unit == 'weight' && unit_value.present? + end + def destruction if is_master? exchange_variants(:reload).destroy_all diff --git a/spec/models/spree/price_spec.rb b/spec/models/spree/price_spec.rb new file mode 100644 index 0000000000..46f6653c77 --- /dev/null +++ b/spec/models/spree/price_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +module Spree + describe Price do + describe "callbacks" do + let(:variant) { create(:variant) } + let(:price) { variant.default_price } + + it "refreshes the products cache on change" do + expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant) + price.amount = 123 + price.save + end + + it "refreshes the products cache on destroy" do + expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant) + price.destroy + end + end + end +end