2016-12-13 12:01:34 +01:00
|
|
|
class CouponExpirationValidator < ActiveModel::Validator
|
|
|
|
##
|
|
|
|
# @param record {Coupon}
|
|
|
|
##
|
|
|
|
def validate(record)
|
|
|
|
previous = record.valid_until_was
|
|
|
|
current = record.valid_until
|
|
|
|
|
|
|
|
unless current.blank?
|
|
|
|
if current.end_of_day < Time.now
|
2016-12-13 12:46:00 +01:00
|
|
|
record.errors[:valid_until] << I18n.t('errors.messages.cannot_be_in_the_past')
|
2016-12-13 12:01:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if !previous.blank? and current.end_of_day < previous.end_of_day
|
2016-12-13 12:46:00 +01:00
|
|
|
record.errors[:valid_until] << I18n.t('errors.messages.cannot_be_before_previous_value')
|
2016-12-13 12:01:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|