Sha256: c9a54607993de7de2b2d47a225c8558f1030b9c49d12385466b774dcc69dd9e6

Contents?: true

Size: 865 Bytes

Versions: 36

Compression:

Stored size: 865 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)
        end
        best_promotion_adjustment.amount
      else
        BigDecimal.new('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

36 entries across 36 versions & 1 rubygems

Version Path
solidus_core-2.3.1 app/models/spree/promotion_chooser.rb
solidus_core-2.4.1 app/models/spree/promotion_chooser.rb
solidus_core-2.2.2 app/models/spree/promotion_chooser.rb
solidus_core-2.1.1 app/models/spree/promotion_chooser.rb
solidus_core-2.0.3 app/models/spree/promotion_chooser.rb
solidus_core-1.4.2 app/models/spree/promotion_chooser.rb
solidus_core-1.3.2 app/models/spree/promotion_chooser.rb
solidus_core-2.4.0 app/models/spree/promotion_chooser.rb
solidus_core-2.4.0.rc1 app/models/spree/promotion_chooser.rb
solidus_core-2.4.0.beta1 app/models/spree/promotion_chooser.rb
solidus_core-2.3.0 app/models/spree/promotion_chooser.rb
solidus_core-2.3.0.rc3 app/models/spree/promotion_chooser.rb
solidus_core-2.3.0.rc2 app/models/spree/promotion_chooser.rb
solidus_core-2.3.0.rc1 app/models/spree/promotion_chooser.rb
solidus_core-2.3.0.beta1 app/models/spree/promotion_chooser.rb
solidus_core-2.0.2 app/models/spree/promotion_chooser.rb
solidus_core-1.4.1 app/models/spree/promotion_chooser.rb
solidus_core-2.2.1 app/models/spree/promotion_chooser.rb
solidus_core-2.2.0 app/models/spree/promotion_chooser.rb
solidus_core-2.2.0.rc1 app/models/spree/promotion_chooser.rb