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.0.9 app/models/spree/stock/availability_validator.rb
spree_core-4.1.15 app/models/spree/stock/availability_validator.rb
spree_core-4.1.14 app/models/spree/stock/availability_validator.rb
spree_core-4.0.8 app/models/spree/stock/availability_validator.rb
spree_core-3.7.14.1 app/models/spree/stock/availability_validator.rb
spree_core-4.1.13.1 app/models/spree/stock/availability_validator.rb
spree_core-4.0.7.1 app/models/spree/stock/availability_validator.rb
spree_core-4.0.7 app/models/spree/stock/availability_validator.rb
spree_core-3.7.14 app/models/spree/stock/availability_validator.rb
spree_core-4.1.13 app/models/spree/stock/availability_validator.rb
spree_core-4.0.6 app/models/spree/stock/availability_validator.rb
spree_core-4.2.0.rc2 app/models/spree/stock/availability_validator.rb
spree_core-3.7.13 app/models/spree/stock/availability_validator.rb
spree_core-4.0.5 app/models/spree/stock/availability_validator.rb
spree_core-4.1.12 app/models/spree/stock/availability_validator.rb
spree_core-3.7.12 app/models/spree/stock/availability_validator.rb
spree_core-4.2.0.rc1 app/models/spree/stock/availability_validator.rb
spree_core-4.0.4 app/models/spree/stock/availability_validator.rb
spree_core-3.7.11 app/models/spree/stock/availability_validator.rb
spree_core-4.1.11 app/models/spree/stock/availability_validator.rb