Sha256: 477aa5d5048262050f9ca2d3f45dc8c42fe65840aa2e4276157dbcd4e6e0ba7d

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module GovukPublishingComponents
  module Presenters
    class ContentBreadcrumbsBasedOnPriority
      # keys are labels, values are the content_ids for the matching taxons
      # Where multiple matching taxons are present, the top most one is the highest priority
      #   and the bottom one the lowest priority
      PRIORITY_TAXONS = {
        education_coronavirus: "272308f4-05c8-4d0d-abc7-b7c2e3ccd249",
        business_coronavirus: "65666cdf-b177-4d79-9687-b9c32805e450",
      }.freeze

      # Returns the highest priority taxon that has a content_id matching those in PRIORITY_TAXONS
      def self.call(content_item)
        new(content_item).taxon
      end

      attr_reader :content_item

      def initialize(content_item)
        @content_item = content_item
      end

      def taxon
        @taxon ||= priority_taxons.min_by { |t| PRIORITY_TAXONS.values.index(t["content_id"]) }
      end

    private

      def priority_taxons
        taxons = content_item.dig("links", "taxons")
        taxon_tree(taxons).select do |taxon|
          priority_taxon?(taxon)
        end
      end

      def taxon_tree(taxons)
        return [] if taxons.blank?

        taxons + taxons.flat_map { |taxon| taxon_tree(taxon.dig("links", "parent_taxons")) }
      end

      def priority_taxon?(taxon)
        PRIORITY_TAXONS.values.include?(taxon["content_id"])
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
govuk_publishing_components-21.52.1 lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb
govuk_publishing_components-21.52.0 lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb