Sha256: 22368cca4b453a0a191bd31d201d89c07a412585146e297c135f0e523777dfa6

Contents?: true

Size: 974 Bytes

Versions: 3

Compression:

Stored size: 974 Bytes

Contents

module GovukNavigationHelpers
  class TaxonBreadcrumbs
    def initialize(content_item)
      @content_item = ContentItem.new(content_item)
    end

    def breadcrumbs
      ordered_parents = all_parents.map.with_index do |parent, index|
        {
          title: parent.title,
          url: parent.base_path,
          is_page_parent: index.zero?
        }
      end

      ordered_parents << {
        title: "Home",
        url: "/",
        is_page_parent: ordered_parents.empty?
}

      ordered_breadcrumbs = ordered_parents.reverse
      ordered_breadcrumbs << { title: content_item.title, is_current_page: true }

      {
        breadcrumbs: ordered_breadcrumbs
      }
    end

  private

    attr_reader :content_item

    def all_parents
      parents = []

      direct_parent = content_item.parent_taxon
      while direct_parent
        parents << direct_parent
        direct_parent = direct_parent.parent_taxon
      end

      parents
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
govuk_navigation_helpers-9.2.0 lib/govuk_navigation_helpers/taxon_breadcrumbs.rb
govuk_navigation_helpers-9.1.0 lib/govuk_navigation_helpers/taxon_breadcrumbs.rb
govuk_navigation_helpers-9.0.0 lib/govuk_navigation_helpers/taxon_breadcrumbs.rb