Class: Spree::Promotion::Actions::CreateItemAdjustments

Inherits:
Spree::PromotionAction show all
Includes:
AdjustmentSource, CalculatedAdjustments
Defined in:
app/models/spree/promotion/actions/create_item_adjustments.rb

Direct Known Subclasses

CreateQuantityAdjustments

Instance Method Summary collapse

Methods inherited from Spree::PromotionAction

#available_calculators, #to_partial_path

Instance Method Details

#compute_amount(adjustable) ⇒ Object

Ensure a negative amount which does not exceed the sum of the order’s item_total and ship_total



36
37
38
39
40
41
42
43
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 36

def compute_amount(adjustable)
  order = adjustable.is_a?(Order) ? adjustable : adjustable.order
  return 0 unless promotion.line_item_actionable?(order, adjustable)
  promotion_amount = calculator.compute(adjustable)
  promotion_amount ||= BigDecimal(0)
  promotion_amount = promotion_amount.abs
  [adjustable.amount, promotion_amount].min * -1
end

#perform(payload = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 22

def perform(payload = {})
  order = payload[:order]
  promotion = payload[:promotion]
  promotion_code = payload[:promotion_code]

  results = line_items_to_adjust(promotion, order).map do |line_item|
    create_adjustment(line_item, order, promotion_code)
  end

  results.any?
end

#preload_relationsObject



18
19
20
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 18

def preload_relations
  [:calculator]
end

#remove_from(order) ⇒ void

This method returns an undefined value.

Removes any adjustments generated by this action from the order’s

line items.

Parameters:

  • order (Spree::Order)

    the order to remove the action from.



49
50
51
52
53
54
55
56
57
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 49

def remove_from(order)
  order.line_items.each do |line_item|
    line_item.adjustments.each do |adjustment|
      if adjustment.source == self
        line_item.adjustments.destroy(adjustment)
      end
    end
  end
end