2025-01-28 14:38:18 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class PollExpirationValidator < ActiveModel::Validator
|
|
|
|
MAX_EXPIRATION = 1.month.freeze
|
|
|
|
MIN_EXPIRATION = 5.minutes.freeze
|
|
|
|
|
|
|
|
def validate(poll)
|
2025-01-29 14:37:59 +00:00
|
|
|
# We have a `presence: true` check for this attribute already
|
|
|
|
return if poll.expires_at.nil?
|
|
|
|
|
2025-01-28 14:38:18 +00:00
|
|
|
current_time = Time.now.utc
|
|
|
|
|
2025-01-29 14:37:59 +00:00
|
|
|
poll.errors.add(:expires_at, I18n.t('polls.errors.duration_too_long')) if poll.expires_at - current_time > MAX_EXPIRATION
|
|
|
|
poll.errors.add(:expires_at, I18n.t('polls.errors.duration_too_short')) if (poll.expires_at - current_time).ceil < MIN_EXPIRATION
|
2025-01-28 14:38:18 +00:00
|
|
|
end
|
|
|
|
end
|