Class: SpreeCmCommissioner::Promotion::Actions::CreateGuestItemAdjustments

Inherits:
Spree::PromotionAction
  • Object
show all
Includes:
Spree::AdjustmentSource, Spree::CalculatedAdjustments
Defined in:
app/models/spree_cm_commissioner/promotion/actions/create_guest_item_adjustments.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.calculatorsObject



11
12
13
# File 'app/models/spree_cm_commissioner/promotion/actions/create_guest_item_adjustments.rb', line 11

def self.calculators
  spree_calculators.promotion_actions_create_item_adjustments
end

Instance Method Details

#compute_amount(line_item) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/spree_cm_commissioner/promotion/actions/create_guest_item_adjustments.rb', line 24

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

  amounts = [line_item.amount, compute_line_item_amount(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

#compute_line_item_amount(line_item) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'app/models/spree_cm_commissioner/promotion/actions/create_guest_item_adjustments.rb', line 36

def compute_line_item_amount(line_item)
  line_item.guests.filter_map do |guest|
    if promotion.guest_eligible?(guest, line_item)
      line_item_per_unit = Struct.new(:amount, :currency, :quantity)
                                 .new(line_item.amount_per_guest, line_item.currency, line_item.quantity)

      compute(line_item_per_unit)
    end
  end.sum
end

#perform(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
# File 'app/models/spree_cm_commissioner/promotion/actions/create_guest_item_adjustments.rb', line 15

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