From 1e038353918abbc713e1fead7827fb948d7ff454 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Thu, 27 Dec 2018 16:04:01 +0100 Subject: [PATCH] Update order while adjustments are open Spree supports updating line items as the model contains these two callbacks: https://github.com/openfoodfoundation/spree/blob/f55722b38db7e706a8521c9091a0e00119bb4d20/core/app/models/spree/line_item.rb#L27-L28 However, as https://github.com/openfoodfoundation/spree/blob/f55722b38db7e706a8521c9091a0e00119bb4d20/core/app/models/spree/adjustment.rb#L1-L7 clearly states, due to Adjustment's state machine, once the order is finalized it can't be updated. Then, if we want to have an up-to-date adjustment_total attribute for a complete order we must update it while the adjustments are open. Otherwise, the following `immutable?` check does not pass. https://github.com/openfoodfoundation/spree/blob/f55722b38db7e706a8521c9091a0e00119bb4d20/core/app/models/spree/adjustment.rb#L84-L92 --- app/models/spree/order_decorator.rb | 2 ++ spec/models/spree/order_spec.rb | 6 ++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 957793d8c2..f7f9852897 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -398,9 +398,11 @@ Spree::Order.class_eval do def update_adjustment!(adjustment) return if adjustment.finalized? + state = adjustment.state adjustment.state = 'open' adjustment.update! + update! adjustment.state = state end diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index ecb966e9d2..0d0cb11be1 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -677,11 +677,9 @@ describe Spree::Order do context "removing line_items" do it "updates shipping and transaction fees" do - # Setting quantity of an item to zero - order.update_attributes(line_items_attributes: [{id: order.line_items.first.id, quantity: 0}]) + order.line_items.first.update_attribute(:quantity, 0) + order.save - # Check if fees got updated - order.reload expect(order.adjustment_total).to eq expected_fees - shipping_fee - payment_fee expect(order.shipment.adjustment.included_tax).to eq 0.6 end