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

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Spree::PromotionAction

#free_shipping?, human_description, #human_description, human_name, #human_name, #key

Class Method Details

.additional_permitted_attributesObject



10
11
12
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 10

def self.additional_permitted_attributes
  [calculator: [:type, { preferences: {} }]]
end

Instance Method Details

#compute_amount(line_item) ⇒ Object



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

def compute_amount(line_item)
  return 0 unless promotion.line_item_actionable?(line_item.order, line_item)

  amounts = [line_item.amount, compute(line_item)]
  order   = line_item.order

  # Prevent negative order totals
  amounts << order.amount - order.adjustments.eligible.sum(:amount).abs if order.adjustments.eligible.any?

  amounts.min * -1
end

#perform(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 14

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

  create_unique_adjustments(order, order.line_items) do |line_item|
    promotion.line_item_actionable?(order, line_item)
  end
end