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
17
18
19
20
21
22
# File 'app/models/spree/stock/availability_validator.rb', line 4

def validate(line_item)
  if shipment = line_item.target_shipment
    units = shipment.inventory_units_for(line_item.variant)
    return if units.count > line_item.quantity
    quantity = line_item.quantity - units.count
  else
    quantity = line_item.quantity
  end

  quantifier = Stock::Quantifier.new(line_item.variant_id)

  unless quantifier.can_supply? quantity
    variant = line_item.variant
    display_name = %Q{#{variant.name}}
    display_name += %Q{ (#{variant.options_text})} unless variant.options_text.blank?

    line_item.errors[:quantity] << Spree.t(:out_of_stock, :scope => :order_populator, :item => display_name.inspect)
  end
end