From 59f39b14f4249236bc91fd3f604103803ade2357 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 23 Jan 2019 10:48:20 +0100 Subject: [PATCH] Do not restore adjustments that aren't in the hash This way we don't modify adjustments that get created as part of the passed block. We don't know how this might be used in the future. --- app/controllers/spree/orders_controller_decorator.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 206a598c33..397b7b966c 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -139,17 +139,21 @@ Spree::OrdersController.class_eval do end # Sets the adjustments to open to perform the block's action and restores - # their state to whatever the they had. + # their state to whatever the they had. Note that it does not change any new + # adjustments that might get created in the yielded block. def with_open_adjustments previous_states = @order.adjustments.each_with_object({}) do |adjustment, hash| - hash[adjustment.id] = adjustment.state + hash[adjustment.id] = { adjustment: adjustment, previous_state: adjustment.state } end @order.adjustments.each(&:open) yield - @order.adjustments.each_with_index do |adjustment, index| - adjustment.update_attribute(:state, previous_states[adjustment.id]) + previous_states.each do |adjustment_id, adjustment_pair| + adjustment = adjustment_pair[:adjustment] + previous_state = adjustment_pair[:previous_state] + + adjustment.update_attribute(:state, previous_state) end end