out_of_office_validator.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Validations::OutOfOfficeValidator < ActiveModel::Validator
  3. def validate(record)
  4. validate_replacement_user(record, required: record.out_of_office)
  5. validate_dates(record, required: record.out_of_office)
  6. end
  7. private
  8. def validate_dates(record, required:)
  9. if record.out_of_office_start_at.blank?
  10. if required
  11. record.errors.add(:out_of_office_start_at, :blank)
  12. end
  13. return
  14. end
  15. if record.out_of_office_end_at.blank?
  16. if required
  17. record.errors.add :out_of_office_end_at, :blank
  18. end
  19. return
  20. end
  21. return if record.out_of_office_start_at <= record.out_of_office_end_at
  22. record.errors.add :base, :less_than_or_equal_to,
  23. message: __('Out of Office start date has to be earlier than or equal to end date')
  24. end
  25. def validate_replacement_user(record, required:)
  26. if !record.out_of_office_replacement_id
  27. if required
  28. record.errors.add :out_of_office_replacement_id, :blank
  29. end
  30. return
  31. end
  32. return if User.exists? record.out_of_office_replacement_id
  33. record.errors.add :out_of_office_replacement_id, :invalid
  34. end
  35. end