2023-02-14 13:10:58 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Check that the expiration date of the given coupon is a valid value
|
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
|
2023-02-14 13:10:58 +01:00
|
|
|
return if current.blank?
|
2016-12-13 12:01:34 +01:00
|
|
|
|
2023-02-14 13:10:58 +01:00
|
|
|
record.errors.add(:valid_until, I18n.t('errors.messages.cannot_be_in_the_past')) if current.end_of_day < Time.current
|
|
|
|
return unless previous.present? && current.end_of_day < previous.end_of_day
|
2016-12-13 12:01:34 +01:00
|
|
|
|
2023-02-14 13:10:58 +01:00
|
|
|
record.errors.add(:valid_until, I18n.t('errors.messages.cannot_be_before_previous_value'))
|
2016-12-13 12:01:34 +01:00
|
|
|
end
|
2023-02-14 13:10:58 +01:00
|
|
|
end
|