2023-02-24 17:26:55 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Validates the validity of a new or updated coupon
|
2016-11-23 12:43:42 +01:00
|
|
|
class CouponDiscountValidator < ActiveModel::Validator
|
|
|
|
def validate(record)
|
|
|
|
if !record.percent_off.nil?
|
2023-02-24 17:26:55 +01:00
|
|
|
record.errors.add(:percent_off, I18n.t('errors.messages.percentage_out_of_range')) unless (0..100).include? record.percent_off
|
2016-11-23 12:43:42 +01:00
|
|
|
elsif !record.amount_off.nil?
|
2023-02-24 17:26:55 +01:00
|
|
|
record.errors.add(:amount_off, I18n.t('errors.messages.greater_than_or_equal_to', **{ count: 0 })) unless record.amount_off.positive?
|
2016-11-23 12:43:42 +01:00
|
|
|
else
|
2023-02-24 17:26:55 +01:00
|
|
|
record.errors.add(: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
|
2023-02-24 17:26:55 +01:00
|
|
|
end
|