2016-11-23 12:43:42 +01:00
|
|
|
class CouponDiscountValidator < ActiveModel::Validator
|
|
|
|
def validate(record)
|
|
|
|
if !record.percent_off.nil?
|
2016-12-13 10:08:19 +01:00
|
|
|
unless (0..100).include? record.percent_off
|
2016-12-13 12:46:00 +01:00
|
|
|
record.errors[:percent_off] << I18n.t('errors.messages.percentage_out_of_range')
|
2016-11-23 12:43:42 +01:00
|
|
|
end
|
|
|
|
elsif !record.amount_off.nil?
|
|
|
|
unless record.amount_off > 0
|
|
|
|
record.errors[:amount_off] << I18n.t('errors.messages.greater_than_or_equal_to', count: 0)
|
|
|
|
end
|
|
|
|
else
|
2016-12-13 12:46:00 +01:00
|
|
|
record.errors[:percent_off] << I18n.t('errors.messages.cannot_be_blank_at_same_time', field: 'amount_off')
|
2016-11-23 12:43:42 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|