From 99c496d268b30f060096aad8fbd88c7083108dcd Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 13 Apr 2022 11:59:43 +0200 Subject: [PATCH] Update in-memory shipments of order in order_shipping Since we update the shipments state in the database, but we read the shipsments from memory in the OrderUpdater#determine_shipment_state we need to set the new values here on the object as well. Co-authored-by: darkswoop --- core/app/models/spree/order_shipping.rb | 5 ++++- core/spec/models/spree/order_shipping_spec.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/core/app/models/spree/order_shipping.rb b/core/app/models/spree/order_shipping.rb index 5aa84375a97..a547ade8b7c 100644 --- a/core/app/models/spree/order_shipping.rb +++ b/core/app/models/spree/order_shipping.rb @@ -70,7 +70,10 @@ def ship(inventory_units:, stock_location:, address:, shipping_method:, # TODO: make OrderShipping#ship_shipment call Shipment#ship! rather than # having Shipment#ship! call OrderShipping#ship_shipment. We only really # need this `update_columns` for the specs, until we make that change. - shipment.update_columns(state: 'shipped', shipped_at: Time.current) + attributes = { state: "shipped", shipped_at: Time.current } + shipment.update_columns(attributes) + # Make sure the in-memory shipments of the order have the correct state for OrderUpdate#determine_shipment_state + @order.shipments.detect { |s| shipment.id == s.id }&.assign_attributes(attributes) end send_shipment_emails(carton) if stock_location.fulfillable? && !suppress_mailer # e.g. digital gift cards that aren't actually shipped diff --git a/core/spec/models/spree/order_shipping_spec.rb b/core/spec/models/spree/order_shipping_spec.rb index 9f481215a53..ec60f5ae615 100644 --- a/core/spec/models/spree/order_shipping_spec.rb +++ b/core/spec/models/spree/order_shipping_spec.rb @@ -214,6 +214,21 @@ def emails end end + context "when a second shipment is shipped" do + let(:order) { create(:order_ready_to_ship) } + + it "sets the order to shipped state" do + order.shipping.ship_shipment(order.shipments.first) + + unshipped_shipment = create(:shipment, order: order, state: "ready") + + order.reload + order.recalculate + + expect { order.shipping.ship_shipment(unshipped_shipment) }.to(change { order.shipment_state }.from("partial").to("shipped")) + end + end + context "when told to suppress the mailer" do subject do order.shipping.ship_shipment(