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::Locale < ApplicationModel
|
|
|
|
belongs_to :knowledge_base, inverse_of: :kb_locales, touch: true
|
|
|
|
belongs_to :system_locale, inverse_of: :knowledge_base_locales, class_name: '::Locale'
|
|
|
|
|
|
|
|
validates :primary, uniqueness: { scope: %i[system_locale_id knowledge_base_id] }, if: :primary?
|
|
|
|
validates :system_locale_id, uniqueness: { scope: :knowledge_base_id }
|
|
|
|
|
|
|
|
has_many :knowledge_base_translations, class_name: 'KnowledgeBase::Translation',
|
|
|
|
inverse_of: :kb_locale,
|
|
|
|
foreign_key: :kb_locale_id,
|
|
|
|
dependent: :destroy
|
|
|
|
|
|
|
|
has_many :category_translations, class_name: 'KnowledgeBase::Category::Translation',
|
|
|
|
inverse_of: :kb_locale,
|
|
|
|
foreign_key: :kb_locale_id,
|
|
|
|
dependent: :destroy
|
|
|
|
|
|
|
|
has_many :answer_translations, class_name: 'KnowledgeBase::Answer::Translation',
|
|
|
|
inverse_of: :kb_locale,
|
|
|
|
foreign_key: :kb_locale_id,
|
|
|
|
dependent: :destroy
|
|
|
|
|
|
|
|
has_many :menu_items, class_name: 'KnowledgeBase::MenuItem',
|
|
|
|
inverse_of: :kb_locale,
|
|
|
|
foreign_key: :kb_locale_id,
|
|
|
|
dependent: :destroy
|
|
|
|
|
|
|
|
def self.system_with_kb_locales(knowledge_base)
|
2020-10-06 18:05:07 +00:00
|
|
|
::Locale
|
2019-06-04 03:40:48 +00:00
|
|
|
.joins(:knowledge_base_locales)
|
|
|
|
.where(knowledge_base_locales: { knowledge_base: knowledge_base })
|
|
|
|
.select('locales.*, knowledge_base_locales.id as kb_locale_id, knowledge_base_locales.primary as primary_locale')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.preferred(user, knowledge_base)
|
|
|
|
preferred_via_system(user, knowledge_base) ||
|
|
|
|
preferred_via_kb(user, knowledge_base) ||
|
|
|
|
knowledge_base.kb_locales.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.preferred_via_system(user, knowledge_base)
|
|
|
|
knowledge_base
|
|
|
|
.kb_locales
|
|
|
|
.joins(:system_locale)
|
|
|
|
.find_by(locales: { locale: user.locale })
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.preferred_via_kb(_user, knowledge_base)
|
|
|
|
knowledge_base.kb_locales.find_by(primary: true)
|
|
|
|
end
|
|
|
|
|
2020-06-22 09:57:45 +00:00
|
|
|
scope :available_for, ->(object) { where(id: object.translations.select(:kb_locale_id)) }
|
2019-06-04 03:40:48 +00:00
|
|
|
end
|