translation.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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::HasUniqueTitle
  6. AGENT_ALLOWED_ATTRIBUTES = %i[title kb_locale_id].freeze
  7. belongs_to :kb_locale, class_name: 'KnowledgeBase::Locale', inverse_of: :category_translations
  8. belongs_to :category, class_name: 'KnowledgeBase::Category', inverse_of: :translations, touch: true
  9. validates :title, presence: true
  10. validates :kb_locale_id, uniqueness: { case_sensitive: true, scope: :category_id }
  11. scope :neighbours_of, ->(translation) { joins(:category).where(knowledge_base_categories: { parent_id: translation.category&.parent_id }) }
  12. def to_param
  13. [category_id, title.parameterize].join('-')
  14. end
  15. def assets(data)
  16. return data if assets_added_to?(data)
  17. data = super
  18. category.assets(data)
  19. end
  20. def search_index_attribute_lookup(include_references: true)
  21. attrs = super
  22. attrs['title'] = ActionController::Base.helpers.strip_tags attrs['title']
  23. attrs['scope_id'] = category.parent_id
  24. attrs
  25. end
  26. scope :search_sql_text_fallback, lambda { |query|
  27. where_or_cis(%w[title], query)
  28. }
  29. scope :apply_kb_scope, lambda { |scope|
  30. if scope.present?
  31. joins(:category)
  32. .where(knowledge_base_categories: { parent_id: scope })
  33. end
  34. }
  35. end