From 81cdc2eac1cf3a691c26e39af3685ec7ddbc38aa Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 10 Feb 2021 17:21:37 +0000 Subject: [PATCH] Bring in Shipment#update_amounts method --- app/models/spree/shipment.rb | 10 ++++++++++ spec/models/spree/shipment_spec.rb | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/models/spree/shipment.rb b/app/models/spree/shipment.rb index 239430e07e..3d31be6973 100644 --- a/app/models/spree/shipment.rb +++ b/app/models/spree/shipment.rb @@ -168,6 +168,15 @@ module Spree !shipped? end + def update_amounts + return unless selected_shipping_rate + + self.update_columns( + cost: selected_shipping_rate.cost, + updated_at: Time.zone.now + ) + end + def manifest inventory_units.group_by(&:variant).map do |variant, units| states = {} @@ -273,6 +282,7 @@ module Spree reload # ensure adjustment is present on later saves end + update_amounts if adjustment update_adjustment_included_tax if adjustment end diff --git a/spec/models/spree/shipment_spec.rb b/spec/models/spree/shipment_spec.rb index 9fecb237b9..0184446869 100644 --- a/spec/models/spree/shipment_spec.rb +++ b/spec/models/spree/shipment_spec.rb @@ -415,7 +415,7 @@ describe Spree::Shipment do allow(shipment). to receive_messages(selected_shipping_rate: Spree::ShippingRate.new(cost: 10.00)) adjustment = build(:adjustment) - allow(shipment).to receive_messages(adjustment: adjustment) + allow(shipment).to receive_messages(adjustment: adjustment, update_columns: true) allow(adjustment).to receive(:open?) { true } expect(shipment.adjustment).to receive(:originator=).with(shipping_method) expect(shipment.adjustment).to receive(:label=).with(shipping_method.adjustment_label) @@ -429,7 +429,7 @@ describe Spree::Shipment do allow(shipment). to receive_messages(selected_shipping_rate: Spree::ShippingRate.new(cost: 10.00)) adjustment = build(:adjustment) - allow(shipment).to receive_messages(adjustment: adjustment) + allow(shipment).to receive_messages(adjustment: adjustment, update_columns: true) allow(adjustment).to receive(:open?) { false } expect(shipment.adjustment).to receive(:originator=).with(shipping_method) expect(shipment.adjustment).to receive(:label=).with(shipping_method.adjustment_label) @@ -447,6 +447,20 @@ describe Spree::Shipment do end end + describe "#update_amounts" do + it "updates shipping cost when selected_shipping_rate is present" do + allow(shipment).to receive(:selected_shipping_rate) { double(:rate, cost: 10) } + expect(shipment).to receive(:update_columns).with(cost: 10, updated_at: kind_of(Time)) + + shipment.update_amounts + end + + it "does nothing when selected_shipping_rate is not present" do + expect(shipment).to_not receive(:update_columns) + shipment.update_amounts + end + end + context "after_save" do it "should run correct callbacks" do expect(shipment).to receive(:ensure_correct_adjustment)