Class: GovukTaxonomyHelpers::LinkedContentItem

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/govuk_taxonomy_helpers/linked_content_item.rb,
lib/govuk_taxonomy_helpers/publishing_api_response.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, base_path:, content_id:, internal_name: nil) ⇒ LinkedContentItem

Returns a new instance of LinkedContentItem.

Parameters:

  • title (String)

    the user facing name for the content item

  • base_path (String)

    the relative URL, starting with a leading "/"

  • content_id (UUID)

    unique identifier of the content item

  • internal_name (String) (defaults to: nil)

    an internal name for the content item



22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



13
14
15
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 13

def base_path
  @base_path
end

#childrenObject (readonly)

Returns the value of attribute children.



13
14
15
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 13

def children
  @children
end

#content_idObject (readonly)

Returns the value of attribute content_id.



13
14
15
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 13

def content_id
  @content_id
end

#internal_nameObject (readonly)

Returns the value of attribute internal_name.



13
14
15
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 13

def internal_name
  @internal_name
end

#parentObject

Returns the value of attribute parent.



14
15
16
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 14

def parent
  @parent
end

#taxonsObject (readonly)

Returns the value of attribute taxons.



15
16
17
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 15

def taxons
  @taxons
end

#titleObject (readonly)

Returns the value of attribute title.



13
14
15
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 13

def title
  @title
end

Class Method Details

.from_content_id(content_id:, publishing_api:) ⇒ LinkedContentItem

Use the publishing API service to fetch and extract a LinkedContentItem

Parameters:

  • content_id (String)

    id of the content

  • publishing_api (PublishingApiV2)

    Publishing API service

Returns:



8
9
10
11
12
13
14
# 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

Instance Method Details

#<<(child_node) ⇒ Object

Add a LinkedContentItem as a child of this one



32
33
34
35
# 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) ⇒ Object

Link this content item to a taxon

Parameters:



100
101
102
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 100

def add_taxon(taxon_node)
  taxons << taxon_node
end

#ancestorsArray

Get ancestors of a taxon

Returns:

  • (Array)

    all taxons in the path from the root of the taxonomy to the parent taxon



59
60
61
62
63
64
65
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 59

def ancestors
  if parent.nil?
    []
  else
    parent.ancestors + [parent]
  end
end

Get a breadcrumb trail for a taxon

Returns:

  • (Array)

    all taxons in the path from the root of the taxonomy to this taxon



70
71
72
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 70

def breadcrumb_trail
  ancestors + [self]
end

#countInteger

Returns the number of taxons in this branch of the taxonomy.

Returns:

  • (Integer)

    the number of taxons in this branch of the taxonomy



82
83
84
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 82

def count
  tree.count
end

#depthInteger

Returns the number of taxons between this taxon and the taxonomy root.

Returns:

  • (Integer)

    the number of taxons between this taxon and the taxonomy root



92
93
94
95
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 92

def depth
  return 0 if root?
  1 + parent.depth
end

#descendantsArray

Get descendants of a taxon

Returns:

  • (Array)

    all taxons in this branch of the taxonomy, excluding the content item itself



52
53
54
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 52

def descendants
  tree.tap(&:shift)
end

#inspectString

Returns the string representation of the content item.

Returns:

  • (String)

    the string representation of the content item



105
106
107
108
109
110
111
# 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?Boolean

Returns whether this taxon is the root of its taxonomy.

Returns:

  • (Boolean)

    whether this taxon is the root of its taxonomy



87
88
89
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 87

def root?
  parent.nil?
end

#taxons_with_ancestorsArray

Get all linked taxons and their ancestors

Returns:

  • (Array)

    all taxons that this content item can be found in



77
78
79
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 77

def taxons_with_ancestors
  taxons.flat_map(&:breadcrumb_trail)
end

#treeArray

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

Returns:

  • (Array)

    all taxons in this branch of the taxonomy, including the content item itself



40
41
42
43
44
45
46
# 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