Module: Spree::AdjustmentSource

Included in:
Promotion::Actions::CreateAdjustment, Promotion::Actions::CreateItemAdjustments, TaxRate
Defined in:
app/models/concerns/spree/adjustment_source.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) deals_with_adjustments_for_deleted_source



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/concerns/spree/adjustment_source.rb', line 3

def deals_with_adjustments_for_deleted_source
  adjustment_scope = adjustments.joins(:order)

  # For incomplete orders, remove the adjustment completely.
  adjustment_scope.where(spree_orders: { completed_at: nil }).destroy_all

  # For complete orders, the source will be invalid.
  # Therefore we nullify the source_id, leaving the adjustment in place.
  # This would mean that the order's total is not altered at all.
  attrs = {
    source_id: nil,
    updated_at: Time.current
  }
  adjustment_scope.where.not(spree_orders: { completed_at: nil }).update_all(attrs)
end