1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-12 23:09:03 +01:00
fab-manager/app/validators/coupon_discount_validator.rb

15 lines
648 B
Ruby
Raw Normal View History

2023-02-24 17:26:55 +01:00
# frozen_string_literal: true
# Validates the validity of a new or updated coupon
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
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?
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' }))
end
end
2023-02-24 17:26:55 +01:00
end