2020-06-15 22:56:38 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ExpiresValidator < ActiveModel::Validator
|
|
|
|
MAX_EXPIRATION = 1.month.freeze
|
|
|
|
MIN_EXPIRATION = 1.minutes.freeze
|
|
|
|
|
|
|
|
def validate(status)
|
|
|
|
current_time = Time.now.utc
|
|
|
|
|
2021-06-28 05:56:26 +00:00
|
|
|
status.errors.add(:expires_at, I18n.t('statuses.errors.duration_too_long')) if status.expires? && status.expires_at - current_time > MAX_EXPIRATION
|
|
|
|
status.errors.add(:expires_at, I18n.t('statuses.errors.duration_too_short')) if status.expires? && (status.expires_at - current_time).ceil < MIN_EXPIRATION
|
2020-06-15 22:56:38 +00:00
|
|
|
end
|
|
|
|
end
|