Sha256: e3a3fb43fae23fd687ef6ca08be99c8963fc35c5b16cd407d990fc320439359a

Contents?: true

Size: 867 Bytes

Versions: 14

Compression:

Stored size: 867 Bytes

Contents

module Spree
  module Stock
    class AvailabilityValidator < ActiveModel::Validator
      def validate(line_item)
        unit_count = line_item.inventory_units.reject(&:pending?).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

14 entries across 14 versions & 1 rubygems

Version Path
spree_core-3.7.0.rc3 app/models/spree/stock/availability_validator.rb
spree_core-3.7.0.rc2 app/models/spree/stock/availability_validator.rb
spree_core-3.7.0.rc1 app/models/spree/stock/availability_validator.rb
spree_core-3.6.6 app/models/spree/stock/availability_validator.rb
spree_core-3.6.5 app/models/spree/stock/availability_validator.rb
spree_core-3.6.4 app/models/spree/stock/availability_validator.rb
spree_core-3.6.3 app/models/spree/stock/availability_validator.rb
spree_core-3.6.2 app/models/spree/stock/availability_validator.rb
spree_core-3.6.1 app/models/spree/stock/availability_validator.rb
spree_core-3.6.0 app/models/spree/stock/availability_validator.rb
spree_core-3.5.0 app/models/spree/stock/availability_validator.rb
spree_core-3.6.0.rc1 app/models/spree/stock/availability_validator.rb
spree_core-3.5.0.rc2 app/models/spree/stock/availability_validator.rb
spree_core-3.5.0.rc1 app/models/spree/stock/availability_validator.rb