Sha256: 8f0478a1e46156db1e9f525db5166d33c4ff913f4676a01699f90baef57e781d

Contents?: true

Size: 887 Bytes

Versions: 6

Compression:

Stored size: 887 Bytes

Contents

module Spree
  class PromotionChooser
    def initialize(adjustments)
      @adjustments = adjustments
    end

    # Picks the best promotion from this set of adjustments, all others are
    # marked as ineligible.
    #
    # @return [BigDecimal] The amount of the best adjustment
    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

    private

    # @return The best promotion from this set of adjustments.
    def best_promotion_adjustment
      @best_promotion_adjustment ||= @adjustments.select(&:eligible?).min_by do |a|
        [a.amount, -a.id]
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
solidus_core-2.5.2 app/models/spree/promotion_chooser.rb
solidus_core-2.5.1 app/models/spree/promotion_chooser.rb
solidus_core-2.5.0 app/models/spree/promotion_chooser.rb
solidus_core-2.5.0.rc1 app/models/spree/promotion_chooser.rb
solidus_core-2.5.0.beta2 app/models/spree/promotion_chooser.rb
solidus_core-2.5.0.beta1 app/models/spree/promotion_chooser.rb