Class: Spree::Stock::AvailabilityValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/spree/stock/availability_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(line_item) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/models/spree/stock/availability_validator.rb', line 4

def validate(line_item)
  units_by_shipment = line_item.inventory_units.group_by(&:shipment)

  if units_by_shipment.blank?
    ensure_in_stock(line_item, line_item.quantity)
  else
    units_by_shipment.each do |shipment, inventory_units|
      ensure_in_stock(line_item, inventory_units.size, shipment.stock_location)
    end
  end

  line_item.errors[:quantity].empty?
end