Sha256: 9e3a617817962edb8683b0199f97e30f8084a68c6862aa686f95e79a124d2c2c

Contents?: true

Size: 1.79 KB

Versions: 448

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

448 entries across 448 versions & 1 rubygems

Version Path
govuk_publishing_components-55.1.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-55.0.1 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-55.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-54.0.1 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-54.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-53.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-52.1.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-52.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-51.2.1 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-51.2.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-51.1.1 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-51.1.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-51.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-50.0.1 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-50.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-49.1.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-49.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-48.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-47.0.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb
govuk_publishing_components-46.4.0 lib/govuk_publishing_components/app_helpers/taxon_breadcrumbs.rb