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.



5
6
7
# File 'app/models/spree/promotion_chooser.rb', line 5

def initialize(adjustments)
  @adjustments = adjustments
end

Instance Method Details

#updateBigDecimal

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

Returns:

  • (BigDecimal)

    The amount of the best adjustment



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

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