Skip to content

Commit 770e527

Browse files
forkataadammathysAlistairNormanstewartbenjaminwil
committed
Mark adjustments for destruction instead of destroying
See code comments and spec changes. This is similar to how we handled this in the legacy promotion system. Co-authored-by: Adam <[email protected]> Co-authored-by: Alistair <[email protected]> Co-authored-by: An <[email protected]> Co-authored-by: Benjamin <[email protected]> Co-authored-by: Harmony <[email protected]> Co-authored-by: Jared <[email protected]> Co-authored-by: Nick <[email protected]> Co-authored-by: Senem <[email protected]> Co-authored-by: Sofia <[email protected]
1 parent de8363e commit 770e527

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

promotions/app/models/solidus_promotions/order_adjuster.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def call(persist: true)
1919
# TODO: - enemy identified
2020
discounted_order = DiscountOrder.new(order, promotions, dry_run: dry_run).call
2121

22-
# TODO: - enemy identified
2322
PersistDiscountedOrder.new(discounted_order).call unless dry_run
2423

2524
order.reset_current_discounts

promotions/app/models/solidus_promotions/order_adjuster/persist_discounted_order.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ def call
3434

3535
attr_reader :order
3636

37-
# Walk through the discounts for an item and update adjustments for it. Once
38-
# all of the discounts have been added as adjustments, remove any old promotion
39-
# adjustments that weren't touched.
37+
# Walk through the discounts for an item and update adjustments for it.
38+
# Once all of the discounts have been added as adjustments, mark for
39+
# destruction any old promotion adjustments that weren't touched.
4040
#
4141
# @private
4242
# @param [#adjustments] item a {Spree::LineItem} or {Spree::Shipment}
43-
# @param [Array<SolidusPromotions::ItemDiscount>] item_discounts a list of calculated discounts for an item
43+
# @param [Array<SolidusPromotions::ItemDiscount>] item_discounts a list of
44+
# calculated discounts for an item
4445
# @return [void]
4546
def update_adjustments(item, item_discounts)
4647
promotion_adjustments = item.adjustments.select(&:promotion?)
@@ -52,7 +53,7 @@ def update_adjustments(item, item_discounts)
5253
# Remove any promotion adjustments tied to promotion benefits which no longer match.
5354
unmatched_adjustments = promotion_adjustments - active_adjustments
5455

55-
item.adjustments.destroy(unmatched_adjustments)
56+
unmatched_adjustments.each(&:mark_for_destruction)
5657
end
5758

5859
# Update or build a new promotion adjustment on an item.

promotions/spec/models/solidus_promotions/order_adjuster_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,12 @@
132132
let(:old_promotion_benefit) { create(:promotion, :with_adjustable_action, apply_automatically: false).actions.first }
133133
let!(:adjustment) { create(:adjustment, source: old_promotion_benefit, adjustable: line_item) }
134134

135-
it "removes the old adjustment from the line item" do
135+
it "marks the old adjustment for destruction" do
136136
adjustable.reload
137137
expect {
138138
subject
139-
}.to change { adjustable.reload.adjustments.length }.by(-1)
139+
}.to change { adjustable.adjustments.first.marked_for_destruction? }
140+
.from(false).to(true)
140141
end
141142
end
142143
end

0 commit comments

Comments
 (0)