knowledge_base_breadcrumb_helper.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module KnowledgeBaseBreadcrumbHelper
  3. def render_breadcrumb_if_needed(knowledge_base, object, alternative)
  4. objects = if object.is_a? Array
  5. calculate_breadcrumb_nonpath(object)
  6. else
  7. calculate_breadcrumb_path(object, alternative)
  8. end
  9. return if objects.empty?
  10. render 'knowledge_base/public/breadcrumb', { objects:, knowledge_base: }
  11. end
  12. def calculate_breadcrumb_path(object, alternative)
  13. objects = calculate_breadcrumb_to_category(object&.parent)
  14. last = if alternative.present? && alternative.translations.any?
  15. Translation.translate(system_locale_via_uri&.locale, 'Alternative Translations')
  16. else
  17. object
  18. end
  19. objects + [last].compact
  20. end
  21. def calculate_breadcrumb_nonpath(object)
  22. [object]
  23. end
  24. def calculate_breadcrumb_to_category(category)
  25. return [] if category.blank?
  26. output = [find_category(category)]
  27. parent = category
  28. while (parent = find_category(parent&.parent_id))
  29. output << parent
  30. end
  31. output.compact.reverse
  32. end
  33. def breadcrumb_path_for(object, locale = params.fetch(:locale))
  34. case object
  35. in KnowledgeBase
  36. help_root_path(locale:)
  37. in KnowledgeBase::Category
  38. help_category_path(object.translation, locale:)
  39. in KnowledgeBase::Answer
  40. help_answer_path(object.category.translation, object.translation, locale:)
  41. in [:tag, tag_name]
  42. help_tag_path(locale, tag_name)
  43. else
  44. nil
  45. end
  46. end
  47. def breadcrumb_text_for(object)
  48. case object
  49. when HasTranslations
  50. object.translation.title
  51. when Array
  52. object[1]
  53. else
  54. object
  55. end
  56. end
  57. end