Module: Spree::Core::AdjustmentSource

Included in:
Promotion::Actions::CreateAdjustment, Promotion::Actions::CreateItemAdjustments, TaxRate
Defined in:
lib/spree/core/adjustment_source.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/spree/core/adjustment_source.rb', line 4

def self.included(klass)
  klass.class_eval do
    def deals_with_adjustments_for_deleted_source
      adjustment_scope = self.adjustments.includes(:order).references(:spree_orders)

      # For incomplete orders, remove the adjustment completely.
      adjustment_scope.where("spree_orders.completed_at IS NULL").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.
      adjustment_scope.where("spree_orders.completed_at IS NOT NULL").each do |adjustment|
        adjustment.update_columns(
          source_id: nil,
          updated_at: Time.now,
        )
      end
    end
  end
end