From 5d828bd7ae5ee5513f219d8f7e669aaacea93f7d Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 25 Jan 2019 14:19:48 +1100 Subject: [PATCH] Update soft-delete of products Spree changed their way of soft-deleting products, variants and some other models. `#destroy` is now soft-deleting and replaces `#delete`. This commit considers only products. Variants will follow in another commit. The other models can be ignored, because we don't call `delete` on them. --- .../spree/api/products_controller_decorator.rb | 2 +- app/models/spree/product_decorator.rb | 8 +++----- .../products_and_inventory_report_spec.rb | 4 ++-- spec/lib/open_food_network/products_cache_spec.rb | 4 ++-- spec/models/spree/product_spec.rb | 14 +++++++++----- spec/requests/shop_spec.rb | 2 +- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/app/controllers/spree/api/products_controller_decorator.rb b/app/controllers/spree/api/products_controller_decorator.rb index 2101ca6f61..a58b4e47b2 100644 --- a/app/controllers/spree/api/products_controller_decorator.rb +++ b/app/controllers/spree/api/products_controller_decorator.rb @@ -33,7 +33,7 @@ Spree::Api::ProductsController.class_eval do authorize! :delete, Spree::Product @product = find_product(params[:product_id]) authorize! :delete, @product - @product.delete + @product.destroy respond_with(@product, :status => 204) end diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index aba22b5abb..8b6914d5bf 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -202,20 +202,18 @@ Spree::Product.class_eval do end end - def delete_with_delete_from_order_cycles + def destroy_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.with_deleted).destroy_all - delete_without_delete_from_order_cycles + destroy_without_delete_from_order_cycles end end end - alias_method_chain :delete, :delete_from_order_cycles + alias_method_chain :destroy, :delete_from_order_cycles def refresh_products_cache diff --git a/spec/lib/open_food_network/products_and_inventory_report_spec.rb b/spec/lib/open_food_network/products_and_inventory_report_spec.rb index 9903b2ebed..1235c9e001 100644 --- a/spec/lib/open_food_network/products_and_inventory_report_spec.rb +++ b/spec/lib/open_food_network/products_and_inventory_report_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' module OpenFoodNetwork - xdescribe ProductsAndInventoryReport do + describe ProductsAndInventoryReport do context "As a site admin" do let(:user) do user = create(:user) @@ -105,7 +105,7 @@ module OpenFoodNetwork it "should filter deleted products" do product1 = create(:simple_product, supplier: supplier) product2 = create(:simple_product, supplier: supplier) - product2.delete + product2.destroy subject.filter(Spree::Variant.scoped).should match_array [product1.master, product1.variants.first] end describe "based on report type" do diff --git a/spec/lib/open_food_network/products_cache_spec.rb b/spec/lib/open_food_network/products_cache_spec.rb index a075d9ec6d..84c06ff2d4 100644 --- a/spec/lib/open_food_network/products_cache_spec.rb +++ b/spec/lib/open_food_network/products_cache_spec.rb @@ -98,7 +98,7 @@ module OpenFoodNetwork it "refreshes the cache based on exchanges the variant was in before destruction" do expect(ProductsCache).to receive(:refresh_cache).with(distributor, oc) - product.delete + product.destroy end it "performs the cache refresh after the product has been removed from the order cycle" do @@ -106,7 +106,7 @@ module OpenFoodNetwork expect(product.reload.deleted_at).not_to be_nil end - product.delete + product.destroy end end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 6f04f97a0c..2d4243eae2 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -172,7 +172,7 @@ module Spree it "refreshes the products cache on delete" do expect(OpenFoodNetwork::ProductsCache).to receive(:product_deleted).with(product) - product.delete + product.destroy end # On destroy, all distributed variants are refreshed by a Variant around_destroy @@ -185,11 +185,15 @@ module Spree 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 } + expect { product.destroy }.to change { supplier.reload.updated_at } end it "touches all distributors" do - expect { product.delete }.to change { distributor.reload.updated_at } + expect { product.destroy }.to change { distributor.reload.updated_at } + end + + it "removes variants from order cycles" do + expect { product.destroy }.to change { ExchangeVariant.count } end end @@ -701,13 +705,13 @@ module Spree it "removes the master variant from all order cycles" do e.variants << p.master - p.delete + p.destroy e.variants(true).should be_empty end it "removes all other variants from order cycles" do e.variants << v - p.delete + p.destroy e.variants(true).should be_empty end end diff --git a/spec/requests/shop_spec.rb b/spec/requests/shop_spec.rb index 1b15c08a7f..359549fa41 100644 --- a/spec/requests/shop_spec.rb +++ b/spec/requests/shop_spec.rb @@ -24,7 +24,7 @@ describe "Shop API", type: :request do set_order order v61.update_attribute(:count_on_hand, 1) - p6.delete + p6.destroy v71.update_attribute(:count_on_hand, 1) v41.update_attribute(:count_on_hand, 1) v42.update_attribute(:count_on_hand, 0)