validator.rb 693 B

1234567891011121314151617181920212223242526272829
  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. return if %i[user_id app key].none? { |column| will_save_change_to_attribute?(column) }
  10. errors.add(:key, :taken) if taskbar_exist?
  11. end
  12. private
  13. def effective_user_id
  14. UserInfo.current_user_id.presence || user_id
  15. end
  16. def taskbar_exist?
  17. clause = { user_id: effective_user_id, app:, key: }
  18. record = Taskbar.where(clause)
  19. id.present? ? record.where.not(id:).exists? : record.exists?
  20. end
  21. end