trabajo-afectivo/app/helpers/knowledge_base_breadcrumb_helper.rb

71 lines
1.8 KiB
Ruby
Raw Normal View History

2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
module KnowledgeBaseBreadcrumbHelper
def render_breadcrumb_if_needed(knowledge_base, object, alternative)
2021-08-16 08:20:07 +00:00
objects = if object.is_a? Array
calculate_breadcrumb_nonpath(object)
else
calculate_breadcrumb_path(object, alternative)
end
return if objects.empty?
render 'knowledge_base/public/breadcrumb',
{
objects: objects,
knowledge_base: knowledge_base
}
end
def calculate_breadcrumb_path(object, alternative)
objects = calculate_breadcrumb_to_category(object&.parent)
last = if alternative.present? && alternative.translations.any?
Translation.translate(system_locale_via_uri&.locale, 'Alternative translations')
else
object
end
objects + [last].compact
end
2021-08-16 08:20:07 +00:00
def calculate_breadcrumb_nonpath(object)
[object]
end
def calculate_breadcrumb_to_category(category)
return [] if category.blank?
output = [category]
parent = category
while (parent = find_category(parent&.parent_id))
output << parent
end
output.compact.reverse
end
def breadcrumb_path_for(object, locale = params.fetch(:locale))
case object
when KnowledgeBase
help_root_path(locale: locale)
when KnowledgeBase::Category
help_category_path(object.translation, locale: locale)
when KnowledgeBase::Answer
help_answer_path(object.category.translation, object.translation, locale: locale)
end
end
def breadcrumb_text_for(object)
case object
when HasTranslations
object.translation.title
2021-08-16 08:20:07 +00:00
when Array
object[1]
else
object
end
end
end