Sha256: 8dc6c25f9ec199c67e69e75634c4d4a566bb21e47fba6a45e45e257fab214aa2

Contents?: true

Size: 945 Bytes

Versions: 90

Compression:

Stored size: 945 Bytes

Contents

# frozen_string_literal: true

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 |adjustment|
        [adjustment.amount, -adjustment.id]
      end
    end
  end
end

Version data entries

90 entries across 90 versions & 2 rubygems

Version Path
solidus_legacy_promotions-4.4.2 app/models/spree/promotion_chooser.rb
solidus_legacy_promotions-4.4.1 app/models/spree/promotion_chooser.rb
solidus_legacy_promotions-4.4.0 app/models/spree/promotion_chooser.rb
solidus_core-4.3.4 app/models/spree/promotion_chooser.rb
solidus_core-4.2.4 app/models/spree/promotion_chooser.rb
solidus_core-4.1.5 app/models/spree/promotion_chooser.rb
solidus_core-4.3.3 app/models/spree/promotion_chooser.rb
solidus_core-4.3.2 app/models/spree/promotion_chooser.rb
solidus_core-4.1.4 app/models/spree/promotion_chooser.rb
solidus_core-4.3.1 app/models/spree/promotion_chooser.rb
solidus_core-4.3.0 app/models/spree/promotion_chooser.rb
solidus_core-4.2.3 app/models/spree/promotion_chooser.rb
solidus_core-4.1.3 app/models/spree/promotion_chooser.rb
solidus_core-4.0.4 app/models/spree/promotion_chooser.rb
solidus_core-3.4.6 app/models/spree/promotion_chooser.rb
solidus_core-4.0.3 app/models/spree/promotion_chooser.rb
solidus_core-4.1.2 app/models/spree/promotion_chooser.rb
solidus_core-4.2.2 app/models/spree/promotion_chooser.rb
solidus_core-3.4.5 app/models/spree/promotion_chooser.rb
solidus_core-4.2.1 app/models/spree/promotion_chooser.rb