2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
class KnowledgeBase::Category::Translation < ApplicationModel
|
|
|
|
include HasAgentAllowedParams
|
|
|
|
include HasSearchIndexBackend
|
|
|
|
include KnowledgeBase::Search
|
|
|
|
include KnowledgeBase::HasUniqueTitle
|
|
|
|
|
|
|
|
AGENT_ALLOWED_ATTRIBUTES = %i[title kb_locale_id].freeze
|
|
|
|
|
2020-06-22 09:57:45 +00:00
|
|
|
belongs_to :kb_locale, class_name: 'KnowledgeBase::Locale', inverse_of: :category_translations
|
2019-06-04 03:40:48 +00:00
|
|
|
validates :kb_locale, presence: true
|
|
|
|
|
|
|
|
belongs_to :category, class_name: 'KnowledgeBase::Category', inverse_of: :translations, touch: true
|
|
|
|
validates :category, presence: true
|
|
|
|
|
|
|
|
validates :title, presence: true
|
2021-06-23 11:35:27 +00:00
|
|
|
validates :kb_locale_id, uniqueness: { case_sensitive: true, scope: :category_id }
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
scope :neighbours_of, ->(translation) { joins(:category).where(knowledge_base_categories: { parent_id: translation.category&.parent_id }) }
|
|
|
|
|
|
|
|
def to_param
|
|
|
|
[category_id, title.parameterize].join('-')
|
|
|
|
end
|
|
|
|
|
|
|
|
def assets(data)
|
|
|
|
return data if assets_added_to?(data)
|
|
|
|
|
|
|
|
data = super(data)
|
|
|
|
category.assets(data)
|
|
|
|
end
|
|
|
|
|
2021-01-27 09:58:35 +00:00
|
|
|
def search_index_attribute_lookup(include_references: true)
|
2019-06-04 03:40:48 +00:00
|
|
|
attrs = super
|
|
|
|
|
|
|
|
attrs['title'] = ActionController::Base.helpers.strip_tags attrs['title']
|
|
|
|
attrs['scope_id'] = category.parent_id
|
|
|
|
|
|
|
|
attrs
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2020-08-05 13:48:41 +00:00
|
|
|
def search_fallback(query, scope = nil, options: {})
|
2019-06-04 03:40:48 +00:00
|
|
|
fields = %w[title]
|
|
|
|
|
|
|
|
output = where_or_cis(fields, query)
|
|
|
|
|
|
|
|
if scope.present?
|
|
|
|
output = output
|
|
|
|
.joins(:category)
|
|
|
|
.where(knowledge_base_categories: { parent_id: scope })
|
|
|
|
end
|
|
|
|
|
|
|
|
output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|