Sha256: f9dda479ab6c6df1bd9ce37039cf22b74c6108a5ce1b7e0c87e5fa0168ee3bc1

Contents?: true

Size: 1.79 KB

Versions: 68

Compression:

Stored size: 1.79 KB

Contents

module GovukPublishingComponents
  module AppHelpers
    class TaxonBreadcrumbs
      # @param content_item A taxon
      def initialize(content_item)
        @content_item = TaxonBreadcrumbs::ContentItem.new(content_item)
      end

      # Generate a breadcrumb trail for a taxon, using the taxon_parent link field
      #
      # @return [Hash] Payload for the GOV.UK breadcrumbs component
      # @see https://govuk-component-guide.herokuapp.com/components/breadcrumbs
      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_parents.reverse
      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

      class ContentItem
        attr_reader :content_item

        def initialize(content_item)
          @content_item = content_item
        end

        def parent_taxon
          @_parent_taxon ||= begin
            parent_content_item = content_item.dig("links", "parent_taxons", 0)

            ContentItem.new(parent_content_item) unless parent_content_item.nil?
          end
        end

        def phase_is_live?(taxon)
          taxon["phase"] == "live"
        end

        def title
          content_item.fetch("title")
        end

        def base_path
          content_item.fetch("base_path")
        end
      end
    end
  end
end

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
govuk_publishing_components-12.5.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-12.4.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-12.3.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-12.2.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-12.1.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-12.0.1 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-12.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-11.2.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-11.1.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-11.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-10.2.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-10.1.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-10.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-9.28.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-9.27.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-9.26.1 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-9.26.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-9.25.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-9.24.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-9.23.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb