From d44c4778e190304136e831a3bee255916e3f76f0 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sun, 23 Dec 2018 09:22:45 +0000 Subject: [PATCH] Improve line item code by merging multiple destroy hooks into a single hook: update_inventory_on_destroy. This makes the code easier to read --- app/models/spree/line_item_decorator.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index b747fd0055..31b4945c51 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -18,8 +18,7 @@ Spree::LineItem.class_eval do before_save :calculate_final_weight_volume, if: :quantity_changed?, unless: :final_weight_volume_changed? after_save :update_units - before_destroy :make_quantity_zero, :update_inventory - after_destroy :reload_order_shipments, :update_order + before_destroy :update_inventory_before_destroy delegate :unit_description, to: :variant @@ -137,16 +136,16 @@ Spree::LineItem.class_eval do end alias_method_chain :update_inventory, :scoping - # This is necessary before destroying the line item - # so that update_inventory will restore stock to the variant - def make_quantity_zero + def update_inventory_before_destroy + # This is necessary before destroying the line item + # so that update_inventory will restore stock to the variant self.quantity = 0 - end - # TThis is necessary after destroying the line item - # because update_inventory may delete the last shipment in the order - # and that makes update_order fail if we don't reload the shipments - def reload_order_shipments + update_inventory + + # This is necessary after updating inventory + # because update_inventory may delete the last shipment in the order + # and that makes update_order fail if we don't reload the shipments order.shipments.reload end