class GovukTaxonomyHelpers::LinkedContentItem

A LinkedContentItem can be anything that has a content store representation on GOV.UK.

It can be used with “taxon” content items (a topic in the taxonomy) or other document types that link to taxons.

Taxon instances can have an optional parent and any number of child taxons.

Attributes

base_path[R]
children[R]
content_id[R]
internal_name[R]
parent[RW]
taxons[R]
title[R]

Public Class Methods

from_content_id(content_id:, publishing_api:) click to toggle source

Use the publishing API service to fetch and extract a LinkedContentItem

@param content_id [String] id of the content @param publishing_api [PublishingApiV2] Publishing API service @return [LinkedContentItem]

# File lib/govuk_taxonomy_helpers/publishing_api_response.rb, line 8
def self.from_content_id(content_id:, publishing_api:)
  PublishingApiResponse.new(
    content_item: publishing_api.get_content(content_id).to_h,
    expanded_links: publishing_api.get_expanded_links(content_id).to_h,
    publishing_api: publishing_api
  ).linked_content_item
end
new(title:, base_path:, content_id:, internal_name: nil) click to toggle source

@param title [String] the user facing name for the content item @param base_path [String] the relative URL, starting with a leading “/” @param content_id [UUID] unique identifier of the content item @param internal_name [String] an internal name for the content item

# File lib/govuk_taxonomy_helpers/linked_content_item.rb, line 22
def initialize(title:, base_path:, content_id:, internal_name: nil)
  @title = title
  @internal_name = internal_name
  @content_id = content_id
  @base_path = base_path
  @children = []
  @taxons = []
end

Public Instance Methods

<<(child_node) click to toggle source

Add a LinkedContentItem as a child of this one

# File lib/govuk_taxonomy_helpers/linked_content_item.rb, line 32
def <<(child_node)
  child_node.parent = self
  @children << child_node
end
add_taxon(taxon_node) click to toggle source

Link this content item to a taxon

@param taxon_node [LinkedContentItem] A taxon content item

# File lib/govuk_taxonomy_helpers/linked_content_item.rb, line 100
def add_taxon(taxon_node)
  taxons << taxon_node
end
ancestors() click to toggle source

Get ancestors of a taxon

@return [Array] all taxons in the path from the root of the taxonomy to the parent taxon

# File lib/govuk_taxonomy_helpers/linked_content_item.rb, line 59
def ancestors
  if parent.nil?
    []
  else
    parent.ancestors + [parent]
  end
end
breadcrumb_trail() click to toggle source

Get a breadcrumb trail for a taxon

@return [Array] all taxons in the path from the root of the taxonomy to this taxon

count() click to toggle source

@return [Integer] the number of taxons in this branch of the taxonomy

# File lib/govuk_taxonomy_helpers/linked_content_item.rb, line 82
def count
  tree.count
end
depth() click to toggle source

@return [Integer] the number of taxons between this taxon and the taxonomy root

# File lib/govuk_taxonomy_helpers/linked_content_item.rb, line 92
def depth
  return 0 if root?
  1 + parent.depth
end
descendants() click to toggle source

Get descendants of a taxon

@return [Array] all taxons in this branch of the taxonomy, excluding the content item itself

# File lib/govuk_taxonomy_helpers/linked_content_item.rb, line 52
def descendants
  tree.tap(&:shift)
end
inspect() click to toggle source

@return [String] the string representation of the content item

# File lib/govuk_taxonomy_helpers/linked_content_item.rb, line 105
def inspect
  if internal_name.nil?
    "LinkedContentItem(title: '#{title}', content_id: '#{content_id}', base_path: '#{base_path}')"
  else
    "LinkedContentItem(title: '#{title}', internal_name: '#{internal_name}', content_id: '#{content_id}', base_path: '#{base_path}')"
  end
end
root?() click to toggle source

@return [Boolean] whether this taxon is the root of its taxonomy

# File lib/govuk_taxonomy_helpers/linked_content_item.rb, line 87
def root?
  parent.nil?
end
taxons_with_ancestors() click to toggle source

Get all linked taxons and their ancestors

@return [Array] all taxons that this content item can be found in

# File lib/govuk_taxonomy_helpers/linked_content_item.rb, line 77
def taxons_with_ancestors
  taxons.flat_map(&:breadcrumb_trail)
end
tree() click to toggle source

Get taxons in the taxon's branch of the taxonomy.

@return [Array] all taxons in this branch of the taxonomy, including the content item itself

# File lib/govuk_taxonomy_helpers/linked_content_item.rb, line 40
def tree
  return [self] if @children.empty?

  @children.each_with_object([self]) do |child, tree|
    tree.concat(child.tree)
  end
end