Sha256: a0f47e50a55bb90297cf36d70fa254342531b4813e3bff9ccda34c244519f379

Contents?: true

Size: 874 Bytes

Versions: 54

Compression:

Stored size: 874 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)
        Spree::Stock::Quantifier.new(line_item.variant).can_supply?(quantity)
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
spree_core-4.1.10 app/models/spree/stock/availability_validator.rb
spree_core-4.2.0.beta app/models/spree/stock/availability_validator.rb
spree_core-4.1.9 app/models/spree/stock/availability_validator.rb
spree_core-4.1.8 app/models/spree/stock/availability_validator.rb
spree_core-4.1.7 app/models/spree/stock/availability_validator.rb
spree_core-4.1.6 app/models/spree/stock/availability_validator.rb
spree_core-4.1.5 app/models/spree/stock/availability_validator.rb
spree_core-4.1.4 app/models/spree/stock/availability_validator.rb
spree_core-4.1.3 app/models/spree/stock/availability_validator.rb
spree_core-4.1.2 app/models/spree/stock/availability_validator.rb
spree_core-4.1.1 app/models/spree/stock/availability_validator.rb
spree_core-4.1.0 app/models/spree/stock/availability_validator.rb
spree_core-4.1.0.rc3 app/models/spree/stock/availability_validator.rb
spree_core-4.1.0.rc2 app/models/spree/stock/availability_validator.rb
spree_core-4.1.0.rc1 app/models/spree/stock/availability_validator.rb
spree_core-3.7.10 app/models/spree/stock/availability_validator.rb
spree_core-4.0.3 app/models/spree/stock/availability_validator.rb
spree_core-3.7.9 app/models/spree/stock/availability_validator.rb
spree_core-4.0.2 app/models/spree/stock/availability_validator.rb
spree_core-3.7.8 app/models/spree/stock/availability_validator.rb