diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index b089c9a52b..e969dd500b 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -22,8 +22,6 @@ Spree::Product.class_eval do attr_accessible :variant_unit, :variant_unit_scale, :variant_unit_name, :unit_value attr_accessible :inherits_properties, :sku - before_validation :sanitize_permalink - # validates_presence_of :variants, unless: :new_record?, message: "Product must have at least one variant" validates_presence_of :supplier validates :primary_taxon, presence: { message: "^Product Category can't be blank" } @@ -35,11 +33,13 @@ Spree::Product.class_eval do validates_presence_of :variant_unit_name, if: -> p { p.variant_unit == 'items' } - after_save :ensure_standard_variant after_initialize :set_available_on_to_now, :if => :new_record? - after_save :update_units - after_touch :touch_distributors + before_validation :sanitize_permalink before_save :add_primary_taxon_to_taxons + after_touch :touch_distributors + after_save :ensure_standard_variant + after_save :update_units + after_save :refresh_products_cache # -- Joins @@ -251,4 +251,8 @@ 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/lib/open_food_network/products_cache.rb b/lib/open_food_network/products_cache.rb index b5e83d7590..eae1a62d15 100644 --- a/lib/open_food_network/products_cache.rb +++ b/lib/open_food_network/products_cache.rb @@ -21,6 +21,10 @@ module OpenFoodNetwork end + def self.product_changed(product) + end + + private def self.exchanges_featuring_variant(variant) diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 25913f679b..75180d8905 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -161,6 +161,19 @@ module Spree end end + describe "callbacks" do + let(:product) { create(:simple_product) } + + it "refreshes the products cache on save" do + expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product) + product.name = 'asdf' + product.save + end + + # On destroy, all distributed variants are refreshed by a Variant around_destroy + # callback, so we don't need to do anything on the product model. + end + describe "scopes" do describe "in_supplier" do it "shows products in supplier" do