Sha256: a49898c90682d9156324cb6505160bed3052b750038a66d9012cf6da0959d028

Contents?: true

Size: 1.08 KB

Versions: 53

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Spree
  module Stock
    class AvailabilityValidator < ActiveModel::Validator
      def validate(line_item)
        if is_valid?(line_item)
          true
        else
          variant = line_item.variant
          display_name = variant.name.to_s
          display_name += %{ (#{variant.options_text})} unless variant.options_text.blank?

          line_item.errors[:quantity] << I18n.t(
            'spree.selected_quantity_not_available',
            item: display_name.inspect
          )
          false
        end
      end

      private

      def is_valid?(line_item)
        if line_item.inventory_units.empty?
          Stock::Quantifier.new(line_item.variant).can_supply?(line_item.quantity)
        else
          quantity_by_stock_location_id = line_item.inventory_units.pending.joins(:shipment).group(:stock_location_id).count
          quantity_by_stock_location_id.all? do |stock_location_id, quantity|
            Stock::Quantifier.new(line_item.variant, stock_location_id).can_supply?(quantity)
          end
        end
      end
    end
  end
end

Version data entries

53 entries across 53 versions & 2 rubygems

Version Path
solidus_core-2.10.5 app/models/spree/stock/availability_validator.rb
solidus_core-2.11.4 app/models/spree/stock/availability_validator.rb
solidus_core-2.11.3 app/models/spree/stock/availability_validator.rb
solidus_core-2.11.2 app/models/spree/stock/availability_validator.rb
solidus_core-2.11.1 app/models/spree/stock/availability_validator.rb
solidus_core-2.10.3 app/models/spree/stock/availability_validator.rb
solidus_core-2.11.0 app/models/spree/stock/availability_validator.rb
solidus_core-2.10.2 app/models/spree/stock/availability_validator.rb
solidus_core-2.9.6 app/models/spree/stock/availability_validator.rb
solidus_core-2.8.6 app/models/spree/stock/availability_validator.rb
solidus_core-2.10.1 app/models/spree/stock/availability_validator.rb
solidus_core-2.9.5 app/models/spree/stock/availability_validator.rb
solidus_core-2.10.0 app/models/spree/stock/availability_validator.rb
solidus_core-2.9.4 app/models/spree/stock/availability_validator.rb
solidus_core-2.6.6 app/models/spree/stock/availability_validator.rb
solidus_core-2.7.4 app/models/spree/stock/availability_validator.rb
solidus_core-2.8.5 app/models/spree/stock/availability_validator.rb
solidus_core-2.9.3 app/models/spree/stock/availability_validator.rb
solidus_core-2.9.2 app/models/spree/stock/availability_validator.rb
solidus_core-2.10.0.beta1 app/models/spree/stock/availability_validator.rb