knowledge_base_breadcrumb_helper.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. module KnowledgeBaseBreadcrumbHelper
  2. def render_breadcrumb_if_needed(knowledge_base, object, alternative)
  3. objects = calculate_breadcrumb_path(object, alternative)
  4. return if objects.empty?
  5. render 'knowledge_base/public/breadcrumb',
  6. {
  7. objects: objects,
  8. knowledge_base: knowledge_base
  9. }
  10. end
  11. def calculate_breadcrumb_path(object, alternative)
  12. objects = calculate_breadcrumb_to_category(object&.parent)
  13. last = if alternative.present? && alternative.translations.any?
  14. Translation.translate(system_locale_via_uri&.locale, 'Alternative translations')
  15. else
  16. object
  17. end
  18. objects + [last].compact
  19. end
  20. def calculate_breadcrumb_to_category(category)
  21. return [] if category.blank?
  22. output = [category]
  23. parent = category
  24. while (parent = find_category(parent&.parent_id))
  25. output << parent
  26. end
  27. output.compact.reverse
  28. end
  29. def breadcrumb_path_for(object, locale = params.fetch(:locale))
  30. case object
  31. when KnowledgeBase
  32. help_root_path(locale: locale)
  33. when KnowledgeBase::Category
  34. help_category_path(object.translation, locale: locale)
  35. when KnowledgeBase::Answer
  36. help_answer_path(object.category.translation, object.translation, locale: locale)
  37. end
  38. end
  39. def breadcrumb_text_for(object)
  40. case object
  41. when HasTranslations
  42. object.translation.title
  43. else
  44. object
  45. end
  46. end
  47. end