translation.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::Category::Translation < ApplicationModel
  3. include HasAgentAllowedParams
  4. include HasSearchIndexBackend
  5. include KnowledgeBase::Search
  6. include KnowledgeBase::HasUniqueTitle
  7. AGENT_ALLOWED_ATTRIBUTES = %i[title kb_locale_id].freeze
  8. belongs_to :kb_locale, class_name: 'KnowledgeBase::Locale', inverse_of: :category_translations
  9. belongs_to :category, class_name: 'KnowledgeBase::Category', inverse_of: :translations, touch: true
  10. validates :title, presence: true
  11. validates :kb_locale_id, uniqueness: { case_sensitive: true, scope: :category_id }
  12. scope :neighbours_of, ->(translation) { joins(:category).where(knowledge_base_categories: { parent_id: translation.category&.parent_id }) }
  13. def to_param
  14. [category_id, title.parameterize].join('-')
  15. end
  16. def assets(data)
  17. return data if assets_added_to?(data)
  18. data = super
  19. category.assets(data)
  20. end
  21. def search_index_attribute_lookup(include_references: true)
  22. attrs = super
  23. attrs['title'] = ActionController::Base.helpers.strip_tags attrs['title']
  24. attrs['scope_id'] = category.parent_id
  25. attrs
  26. end
  27. class << self
  28. def search_fallback(query, scope = nil, options: {})
  29. fields = %w[title]
  30. output = where_or_cis(fields, query)
  31. if scope.present?
  32. output = output
  33. .joins(:category)
  34. .where(knowledge_base_categories: { parent_id: scope })
  35. end
  36. output
  37. end
  38. end
  39. end