Class: Spree::PromotionChooser

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/promotion_chooser.rb

Instance Method Summary collapse

Constructor Details

#initialize(adjustments) ⇒ PromotionChooser

Returns a new instance of PromotionChooser.



3
4
5
# File 'app/models/spree/promotion_chooser.rb', line 3

def initialize(adjustments)
  @adjustments = adjustments
end

Instance Method Details

#best_promotion_adjustmentObject

Returns The best promotion from this set of adjustments.

Returns:

  • The best promotion from this set of adjustments.



24
25
26
27
28
# File 'app/models/spree/promotion_chooser.rb', line 24

def best_promotion_adjustment
  @best_promotion_adjustment ||= @adjustments.select(&:eligible?).min_by do |a|
    [a.amount, -a.id]
  end
end

#updateBigDecimal

Picks the best promotion from this set of adjustments, all others are marked as ineligible.

Returns:

  • (BigDecimal)

    The amount of the best adjustment



11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/spree/promotion_chooser.rb', line 11

def update
  if best_promotion_adjustment
    @adjustments.select(&:eligible?).each do |adjustment|
      next if adjustment == best_promotion_adjustment
      adjustment.update_columns(eligible: false)
    end
    best_promotion_adjustment.amount
  else
    BigDecimal.new('0')
  end
end