Sha256: b957084339be247aa518483cc4a234f7ddd25b1498e69629370c3f133daf2dc7

Contents?: true

Size: 1.05 KB

Versions: 16

Compression:

Stored size: 1.05 KB

Contents

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] << Spree.t(
            :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

16 entries across 16 versions & 1 rubygems

Version Path
solidus_core-2.4.2 app/models/spree/stock/availability_validator.rb
solidus_core-2.3.1 app/models/spree/stock/availability_validator.rb
solidus_core-2.4.1 app/models/spree/stock/availability_validator.rb
solidus_core-2.2.2 app/models/spree/stock/availability_validator.rb
solidus_core-2.4.0 app/models/spree/stock/availability_validator.rb
solidus_core-2.4.0.rc1 app/models/spree/stock/availability_validator.rb
solidus_core-2.4.0.beta1 app/models/spree/stock/availability_validator.rb
solidus_core-2.3.0 app/models/spree/stock/availability_validator.rb
solidus_core-2.3.0.rc3 app/models/spree/stock/availability_validator.rb
solidus_core-2.3.0.rc2 app/models/spree/stock/availability_validator.rb
solidus_core-2.3.0.rc1 app/models/spree/stock/availability_validator.rb
solidus_core-2.3.0.beta1 app/models/spree/stock/availability_validator.rb
solidus_core-2.2.1 app/models/spree/stock/availability_validator.rb
solidus_core-2.2.0 app/models/spree/stock/availability_validator.rb
solidus_core-2.2.0.rc1 app/models/spree/stock/availability_validator.rb
solidus_core-2.2.0.beta1 app/models/spree/stock/availability_validator.rb