Sha256: 1d6bb4adbf980961b34575f2b6c05b5ede5042db204e1e6e5930128af14c16cc

Contents?: true

Size: 848 Bytes

Versions: 9

Compression:

Stored size: 848 Bytes

Contents

module Spree
  module Stock
    class AvailabilityValidator < ActiveModel::Validator
      def validate(line_item)
        unit_count = line_item.inventory_units.sum(&:quantity)
        return if unit_count >= line_item.quantity

        quantity = line_item.quantity - unit_count
        return if quantity.zero?

        return if item_available?(line_item, quantity)

        variant = line_item.variant
        display_name = variant.name.to_s
        display_name += " (#{variant.options_text})" unless variant.options_text.blank?

        line_item.errors[:quantity] << Spree.t(
          :selected_quantity_not_available,
          item: display_name.inspect
        )
      end

      private

      def item_available?(line_item, quantity)
        Stock::Quantifier.new(line_item.variant).can_supply?(quantity)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spree_core-3.4.6 app/models/spree/stock/availability_validator.rb
spree_core-3.4.5 app/models/spree/stock/availability_validator.rb
spree_core-3.4.4 app/models/spree/stock/availability_validator.rb
spree_core-3.4.3 app/models/spree/stock/availability_validator.rb
spree_core-3.4.2 app/models/spree/stock/availability_validator.rb
spree_core-3.4.1 app/models/spree/stock/availability_validator.rb
spree_core-3.4.0 app/models/spree/stock/availability_validator.rb
spree_core-3.4.0.rc2 app/models/spree/stock/availability_validator.rb
spree_core-3.4.0.rc1 app/models/spree/stock/availability_validator.rb