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

#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, updated_at: Time.current)
    end
    best_promotion_adjustment.amount
  else
    BigDecimal.new('0')
  end
end