Use Spree::ItemAdjustments in Shipment callbacks for updating adjustments

This commit is contained in:
Matt-Yorkley
2021-02-07 13:10:07 +00:00
parent ac9ecdfcbc
commit 605a94e3c9
2 changed files with 10 additions and 11 deletions

View File

@@ -15,7 +15,7 @@ module Spree
has_many :adjustments, as: :adjustable, dependent: :destroy
before_create :generate_shipment_number
after_save :ensure_correct_adjustment, :update_order
after_save :ensure_correct_adjustment, :update_adjustments
attr_accessor :special_instructions
alias_attribute :amount, :cost
@@ -344,8 +344,14 @@ module Spree
end
end
def update_order
order.reload.update!
def update_adjustments
return unless cost_changed? && state != 'shipped'
recalculate_adjustments
end
def recalculate_adjustments
Spree::ItemAdjustments.new(self).update
end
end
end

View File

@@ -423,13 +423,6 @@ describe Spree::Shipment do
end
end
context "update_order" do
it "should update order" do
expect(order).to receive_message_chain(:reload, :update!)
shipment.__send__(:update_order)
end
end
describe "#update_amounts" do
it "persists the shipping cost from the shipping fee adjustment" do
allow(shipment).to receive(:fee_adjustment) { double(:adjustment, amount: 10) }
@@ -442,7 +435,7 @@ describe Spree::Shipment do
context "after_save" do
it "should run correct callbacks" do
expect(shipment).to receive(:ensure_correct_adjustment)
expect(shipment).to receive(:update_order)
expect(shipment).to receive(:update_adjustments)
shipment.run_callbacks(:save)
end
end