tap/app/validators/in_stock_validator.rb
2015-02-12 14:39:58 +01:00

10 lines
352 B
Ruby

class InStockValidator < ActiveModel::Validator
def validate(record)
p_short = []
record.order_items.each do |oi|
p_short.append oi.product.name if oi.count > oi.product.stock
end
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