1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-05 16:24:21 +01:00
fab-manager/app/validators/period_overlap_validator.rb

22 lines
761 B
Ruby

# frozen_string_literal: true
# Validates the current accounting period does not overlap an existing one
class PeriodOverlapValidator < ActiveModel::Validator
def validate(record)
the_end = record.end_at
the_start = record.start_at
AccountingPeriod.all.each do |period|
if the_start >= period.start_at && the_start <= period.end_at
record.errors[:start_at] << I18n.t('errors.messages.cannot_overlap')
end
if the_end >= period.start_at && the_end <= period.end_at
record.errors[:end_at] << I18n.t('errors.messages.cannot_overlap')
end
if period.start_at >= the_start && period.end_at <= the_end
record.errors[:end_at] << I18n.t('errors.messages.cannot_encompass')
end
end
end
end