Sha256: a9d906c33f6049dac64cbff242cdd75a4fd561a4630ce51922a5a35236381411
Contents?: true
Size: 848 Bytes
Versions: 11
Compression:
Stored size: 848 Bytes
Contents
module Spree module Stock class AvailabilityValidator < ActiveModel::Validator def validate(line_item) unit_count = line_item.inventory_units.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}" 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
11 entries across 11 versions & 1 rubygems