Bring in Shipment#update_amounts method

This commit is contained in:
Matt-Yorkley
2021-02-10 17:21:37 +00:00
parent 2a0f132b8b
commit 81cdc2eac1
2 changed files with 26 additions and 2 deletions

View File

@@ -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

View File

@@ -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)