Sha256: 9f0b27469d50858b9a335e0e88698fff996e90cf7d770ec15cb2832c62f8af37

Contents?: true

Size: 751 Bytes

Versions: 9

Compression:

Stored size: 751 Bytes

Contents

module Spree
  module Stock
    class AvailabilityValidator < ActiveModel::Validator
      def validate(line_item)
        unit_count = line_item.inventory_units.size
        return if unit_count >= line_item.quantity
        quantity = line_item.quantity - unit_count
        return if quantity.zero?

        quantifier = Stock::Quantifier.new(line_item.variant)

        return if quantifier.can_supply?(quantity)

        variant = line_item.variant
        display_name = "#{variant.name}"
        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
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spree_core-3.0.4 app/models/spree/stock/availability_validator.rb
spree_core-2.4.10 app/models/spree/stock/availability_validator.rb
spree_core-2.3.13 app/models/spree/stock/availability_validator.rb
spree_core-3.0.3 app/models/spree/stock/availability_validator.rb
spree_core-2.4.9 app/models/spree/stock/availability_validator.rb
spree_core-2.3.12 app/models/spree/stock/availability_validator.rb
spree_core-3.0.2 app/models/spree/stock/availability_validator.rb
spree_core-2.4.8 app/models/spree/stock/availability_validator.rb
spree_core-2.3.11 app/models/spree/stock/availability_validator.rb