knowledge_base_breadcrumb_helper.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (C) 2012-2023 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 = [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. when KnowledgeBase
  36. help_root_path(locale:)
  37. when KnowledgeBase::Category
  38. help_category_path(object.translation, locale:)
  39. when KnowledgeBase::Answer
  40. help_answer_path(object.category.translation, object.translation, locale:)
  41. end
  42. end
  43. def breadcrumb_text_for(object)
  44. case object
  45. when HasTranslations
  46. object.translation.title
  47. when Array
  48. object[1]
  49. else
  50. object
  51. end
  52. end
  53. end