2019-04-03 13:04:19 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Validates that the duration between start_at and end_at is between 1 day and 1 year
|
|
|
|
class DurationValidator < ActiveModel::Validator
|
|
|
|
def validate(record)
|
|
|
|
the_end = record.end_at
|
|
|
|
the_start = record.start_at
|
|
|
|
diff = (the_end - the_start).to_i
|
2019-04-03 17:27:25 +02:00
|
|
|
# 0.day means that (the_start == the_end), so it's a one day period
|
2023-02-24 17:26:55 +01:00
|
|
|
return if diff.days >= 0.days && diff.days <= 1.year
|
2019-04-03 13:04:19 +02:00
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
record.errors.add(:end_at, I18n.t('errors.messages.invalid_duration', **{ DAYS: diff }))
|
2019-04-03 13:04:19 +02:00
|
|
|
end
|
|
|
|
end
|