From be6c8be07aa6a1f7ef2e33842a3ce49a55407068 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 14 Dec 2018 15:13:06 +0100 Subject: [PATCH] Handle soft deleted products when refreshing cache Since cd3add960e Spree soft-deletes products and as such the models not destroyed but updated setting a value for the `deleted_at` field. This turns what used to be deletes into updates thus triggering AR's update callbacks instead of the destroy ones. As a result, this bypasses the logic to refresh the products cache on destroy and hits the `after_save` callback. Furthermore, since act_as_paranoid (the soft-delete gem Spree uses) uses a default scope to avoid retrieving soft-deleted records we need to purposefully fetch them in order to refresh the cache. --- app/models/spree/classification_decorator.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/spree/classification_decorator.rb b/app/models/spree/classification_decorator.rb index 32e94235cf..306e968a3f 100644 --- a/app/models/spree/classification_decorator.rb +++ b/app/models/spree/classification_decorator.rb @@ -1,14 +1,14 @@ Spree::Classification.class_eval do - belongs_to :product, :class_name => "Spree::Product", touch: true - after_save :refresh_products_cache + belongs_to :product, class_name: "Spree::Product", touch: true + before_destroy :dont_destroy_if_primary_taxon after_destroy :refresh_products_cache - + after_save :refresh_products_cache private def refresh_products_cache - product.refresh_products_cache + Spree::Product.with_deleted.find(product_id).refresh_products_cache end def dont_destroy_if_primary_taxon