tap/app/validators/in_stock_validator.rb

10 lines
352 B
Ruby
Raw Normal View History

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
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
end
end