has_unique_title.rb 583 B

12345678910111213141516171819202122232425
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # requires scope "neighbors_of" to find translations in same scope
  3. class KnowledgeBase
  4. module HasUniqueTitle
  5. extend ActiveSupport::Concern
  6. included do
  7. validate :validate_title_uniqueness
  8. end
  9. private
  10. def validate_title_uniqueness
  11. return if self
  12. .class
  13. .where(kb_locale_id: kb_locale_id, title: title)
  14. .where.not(id: id)
  15. .neighbours_of(self)
  16. .none?
  17. errors.add(:title, __('is already used'))
  18. end
  19. end
  20. end