validator.rb 603 B

12345678910111213141516171819202122232425262728
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Taskbar::Validator
  3. extend ActiveSupport::Concern
  4. included do
  5. validate :validate_uniqueness, on: %i[create update]
  6. end
  7. def validate_uniqueness
  8. return if local_update
  9. errors.add(:key, :taken) if taskbar_exist?
  10. end
  11. private
  12. def effective_user_id
  13. UserInfo.current_user_id.presence || user_id
  14. end
  15. def taskbar_exist?
  16. clause = { user_id: effective_user_id, app:, key: }
  17. record = Taskbar.where(clause)
  18. id.present? ? record.where.not(id:).present? : record.present?
  19. end
  20. end