Sha256: 40085c34a24459898021f4c44492ec4e5d0f3697f6075242bdf010d23565ebdb

Contents?: true

Size: 837 Bytes

Versions: 39

Compression:

Stored size: 837 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?

        return if item_available?(line_item, 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

      private

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

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
spree_core-3.2.9 app/models/spree/stock/availability_validator.rb
spree_core-3.1.14 app/models/spree/stock/availability_validator.rb
spree_core-3.2.8 app/models/spree/stock/availability_validator.rb
spree_core-3.1.13 app/models/spree/stock/availability_validator.rb
spree_core-3.2.7 app/models/spree/stock/availability_validator.rb
spree_core-3.1.12 app/models/spree/stock/availability_validator.rb
spree_core-3.2.6 app/models/spree/stock/availability_validator.rb
spree_core-3.1.11 app/models/spree/stock/availability_validator.rb
spree_core-3.1.10 app/models/spree/stock/availability_validator.rb
spree_core-3.2.5 app/models/spree/stock/availability_validator.rb
spree_core-3.1.9 app/models/spree/stock/availability_validator.rb
spree_core-3.2.4 app/models/spree/stock/availability_validator.rb
spree_core-3.1.8 app/models/spree/stock/availability_validator.rb
spree_core-3.1.7 app/models/spree/stock/availability_validator.rb
spree_core-3.2.3 app/models/spree/stock/availability_validator.rb
spree_core-3.2.2 app/models/spree/stock/availability_validator.rb
spree_core-3.1.6 app/models/spree/stock/availability_validator.rb
spree_core-3.1.5 app/models/spree/stock/availability_validator.rb
spree_core-3.2.1 app/models/spree/stock/availability_validator.rb
spree_core-3.2.0 app/models/spree/stock/availability_validator.rb