diff --git a/app/models/spree/classification_decorator.rb b/app/models/spree/classification_decorator.rb index c69eb01fcf..5e9655a907 100644 --- a/app/models/spree/classification_decorator.rb +++ b/app/models/spree/classification_decorator.rb @@ -1,6 +1,15 @@ Spree::Classification.class_eval do belongs_to :product, :class_name => "Spree::Product", touch: true + after_save :refresh_products_cache before_destroy :dont_destroy_if_primary_taxon + after_destroy :refresh_products_cache + + + private + + def refresh_products_cache + product.refresh_products_cache + end def dont_destroy_if_primary_taxon if product.primary_taxon == taxon diff --git a/app/models/spree/taxon_decorator.rb b/app/models/spree/taxon_decorator.rb index 1a26ce73a8..a1e07cc53e 100644 --- a/app/models/spree/taxon_decorator.rb +++ b/app/models/spree/taxon_decorator.rb @@ -1,7 +1,12 @@ Spree::Taxon.class_eval do + has_many :classifications, :dependent => :destroy + + self.attachment_definitions[:icon][:path] = 'public/images/spree/taxons/:id/:style/:basename.:extension' self.attachment_definitions[:icon][:url] = '/images/spree/taxons/:id/:style/:basename.:extension' + after_save :refresh_products_cache + # Indicate which filters should be used for this taxon def applicable_filters @@ -45,4 +50,11 @@ Spree::Taxon.class_eval do taxons end + + + private + + def refresh_products_cache + products(:reload).each &:refresh_products_cache + end end diff --git a/spec/models/spree/classification_spec.rb b/spec/models/spree/classification_spec.rb index f26f6da0c0..44449ee54e 100644 --- a/spec/models/spree/classification_spec.rb +++ b/spec/models/spree/classification_spec.rb @@ -2,8 +2,8 @@ require 'spec_helper' module Spree describe Classification do - let(:product) { create(:simple_product) } - let(:taxon) { create(:taxon) } + let!(:product) { create(:simple_product) } + let!(:taxon) { create(:taxon) } let(:classification) { create(:classification, taxon: taxon, product: product) } it "won't destroy if classification is the primary taxon" do @@ -11,5 +11,18 @@ module Spree classification.destroy.should be_false classification.errors.messages[:base].should == ["Taxon #{taxon.name} is the primary taxon of #{product.name} and cannot be deleted"] end + + describe "callbacks" do + it "refreshes the products cache on save" do + expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product) + classification + end + + it "refreshes the products cache on destroy" do + classification + expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product) + classification.destroy + end + end end end diff --git a/spec/models/spree/taxon_spec.rb b/spec/models/spree/taxon_spec.rb index a0d729c054..926e5b3c5f 100644 --- a/spec/models/spree/taxon_spec.rb +++ b/spec/models/spree/taxon_spec.rb @@ -7,6 +7,21 @@ module Spree let(:t1) { create(:taxon) } let(:t2) { create(:taxon) } + describe "callbacks" do + let(:product) { create(:simple_product, taxons: [t1]) } + + it "refreshes the products cache on save" do + expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product) + t1.name = 'asdf' + t1.save + end + + it "refreshes the products cache on destroy" do + expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product) + t1.destroy + end + end + describe "finding all supplied taxons" do let!(:p1) { create(:simple_product, supplier: e, taxons: [t1, t2]) }