class GovukPublishingComponents::Presenters::ContentBreadcrumbsBasedOnPriority

Constants

BREXIT_TAXONS

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

Attributes

content_item[R]
query_parameters[R]

Public Class Methods

call(content_item, query_parameters = nil) click to toggle source

Returns the highest priority taxon that has a content_id matching those in PRIORITY_TAXONS

# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 20
def self.call(content_item, query_parameters = nil)
  new(content_item, query_parameters).breadcrumbs
end
new(content_item, query_parameters = nil) click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 26
def initialize(content_item, query_parameters = nil)
  @content_item = content_item
  @query_parameters = query_parameters
end

Public Instance Methods

breadcrumbs() click to toggle source
brexit_audience(taxon) click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 57
def brexit_audience(taxon)
  return nil unless taxon

  {
    PRIORITY_TAXONS[:brexit_business] => "Brexitbusiness",
    PRIORITY_TAXONS[:brexit_individuals] => "Brexitcitizen",
    PRIORITY_TAXONS[:brexit_taxon] => "Brexitbusinessandcitizen",
  }[taxon["content_id"]]
end
priority_brexit_taxon() click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 38
def priority_brexit_taxon
  if tagged_to_both_brexit_child_taxons?
    priority_brexit_taxons.find { |t| t["content_id"] == BREXIT_TAXONS[:brexit_taxon] }
  else
    priority_brexit_taxons.min_by { |t| BREXIT_TAXONS.values.index(t["content_id"]) }
  end
end
priority_taxon() click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 31
def priority_taxon
  @priority_taxon ||= begin
    default_taxon = priority_taxons.min_by { |t| PRIORITY_TAXONS.values.index(t["content_id"]) }
    preferred_taxon || default_taxon
  end
end

Private Instance Methods

breadcrumb_path() click to toggle source
brexit_child_taxon?(taxon) click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 104
def brexit_child_taxon?(taxon)
  brexit_child_taxons.include?(taxon["content_id"])
end
brexit_child_taxons() click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 108
def brexit_child_taxons
  [PRIORITY_TAXONS[:brexit_business], PRIORITY_TAXONS[:brexit_individuals]]
end
preferred_priority_taxon() click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 112
def preferred_priority_taxon
  query_parameters["priority-taxon"] if query_parameters
end
preferred_taxon() click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 69
def preferred_taxon
  if preferred_priority_taxon
    priority_taxons.find { |t| t["content_id"] == preferred_priority_taxon }
  elsif tagged_to_both_brexit_child_taxons?
    priority_taxons.find { |t| t["content_id"] == PRIORITY_TAXONS[:brexit_taxon] }
  end
end
priority_brexit_taxon?(taxon) click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 100
def priority_brexit_taxon?(taxon)
  BREXIT_TAXONS.values.include?(taxon["content_id"])
end
priority_brexit_taxons() click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 86
def priority_brexit_taxons
  priority_taxons.select { |t| priority_brexit_taxon?(t) }
end
priority_taxon?(taxon) click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 96
def priority_taxon?(taxon)
  PRIORITY_TAXONS.values.include?(taxon["content_id"])
end
priority_taxons() click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 77
def priority_taxons
  return [] unless content_item["links"].is_a?(Hash)

  taxons = content_item.dig("links", "taxons")
  taxon_tree(taxons).select do |taxon|
    priority_taxon?(taxon)
  end
end
tagged_to_both_brexit_child_taxons?() click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 122
def tagged_to_both_brexit_child_taxons?
  t = priority_taxons.select { |taxon| brexit_child_taxon?(taxon) }
  t.uniq.count > 1
end
taxon_tree(taxons) click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 90
def taxon_tree(taxons)
  return [] if taxons.blank?

  taxons + taxons.flat_map { |taxon| taxon_tree(taxon.dig("links", "parent_taxons")) }
end
tracking_action() click to toggle source
# File lib/govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority.rb, line 116
def tracking_action
  action = %w[superBreadcrumb]
  action << brexit_audience(priority_taxon)
  action.compact.join(" ")
end