2015-01-15 00:39:34 +01:00
|
|
|
class InStockValidator < ActiveModel::Validator
|
|
|
|
def validate(record)
|
2015-02-12 14:39:58 +01:00
|
|
|
p_short = []
|
2015-02-09 17:06:24 +01:00
|
|
|
record.order_items.each do |oi|
|
2015-02-12 14:39:58 +01:00
|
|
|
p_short.append oi.product.name if oi.count > oi.product.stock
|
2015-01-15 00:39:34 +01:00
|
|
|
end
|
2015-02-12 14:39:58 +01:00
|
|
|
record.errors.add(:base, "There is not enough stock for your order of the following products: #{p_short.join(', ')}") if p_short.size > 0
|
2015-01-15 00:39:34 +01:00
|
|
|
end
|
|
|
|
end
|