Sha256: 357d77c78633b9fc0e63ee281578fe42d834fbd8c8a6b61e6f9936485be3f2d9
Contents?: true
Size: 918 Bytes
Versions: 43
Compression:
Stored size: 918 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 |a| [a.amount, -a.id] end end end end
Version data entries
43 entries across 43 versions & 2 rubygems