From 57363e2da55878add81c794a510ff7e72e7541d0 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 25 Nov 2016 10:53:08 +1100 Subject: [PATCH] When a product is deleted, touch the supplier and distributors --- app/models/spree/product_decorator.rb | 4 ++++ spec/models/spree/product_spec.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index d7f5651a3c..40c6a6910b 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -188,6 +188,10 @@ Spree::Product.class_eval do def delete_with_delete_from_order_cycles transaction do OpenFoodNetwork::ProductsCache.product_deleted(self) do + # Touch supplier and distributors as we would on #destroy + self.supplier.touch + touch_distributors + ExchangeVariant.where('exchange_variants.variant_id IN (?)', self.variants_including_master_and_deleted).destroy_all delete_without_delete_from_order_cycles diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 4d126bd80f..568d701ec9 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -177,6 +177,21 @@ module Spree # 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. + + describe "touching affected enterprises when the product is deleted" do + let(:product) { create(:simple_product) } + let(:supplier) { product.supplier } + let(:distributor) { create(:distributor_enterprise) } + let!(:oc) { create(:simple_order_cycle, distributors: [distributor], variants: [product.variants.first]) } + + it "touches the supplier" do + expect { product.delete }.to change { supplier.reload.updated_at } + end + + it "touches all distributors" do + expect { product.delete }.to change { distributor.reload.updated_at } + end + end end describe "scopes" do