Deal with edge cases where an adjustment is left referencing a deleted item

This commit is contained in:
Matt-Yorkley
2021-10-04 17:55:27 +01:00
parent 43f28d94b9
commit 65f08ee8f4
2 changed files with 22 additions and 0 deletions

View File

@@ -100,6 +100,11 @@ module Spree
def update_adjustment!(calculable = nil, force: false)
return amount if immutable? && !force
if calculable.nil? && adjustable.nil?
self.delete
return 0.0
end
if originator.present?
amount = originator.compute_amount(calculable || adjustable)
update_columns(

View File

@@ -64,6 +64,23 @@ module Spree
expect(adjustment).not_to receive(:update_columns)
adjustment.update_adjustment!
end
context "where the adjustable has been deleted" do
let(:line_item) { create(:line_item, price: 10) }
let!(:adjustment) { create(:adjustment, adjustable: line_item) }
it "returns zero" do
line_item.delete
expect(adjustment.reload.update_adjustment!).to eq 0.0
end
it "removes orphaned adjustments" do
expect {
line_item.delete
adjustment.reload.update_adjustment!
}.to change{ Spree::Adjustment.count }.by -1
end
end
end
context "adjustment state" do