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

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

Instance Method Summary collapse

Methods inherited from Base

belongs_to_required_by_default, page, spree_base_scopes

Methods included from Spree::Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Method Details

#compute_amount(line_item) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 19

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.sum(:amount).abs if order.adjustments.any?

  amounts.min * -1
end

#perform(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 10

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