Class: GovukTaxonomyHelpers::LinkedContentItem
- Inherits:
-
Object
- Object
- GovukTaxonomyHelpers::LinkedContentItem
- 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
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#content_id ⇒ Object
readonly
Returns the value of attribute content_id.
-
#internal_name ⇒ Object
readonly
Returns the value of attribute internal_name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#taxons ⇒ Object
readonly
Returns the value of attribute taxons.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
-
.from_content_id(content_id:, publishing_api:) ⇒ LinkedContentItem
Use the publishing API service to fetch and extract a LinkedContentItem.
Instance Method Summary collapse
-
#<<(child_node) ⇒ Object
Add a LinkedContentItem as a child of this one.
-
#add_taxon(taxon_node) ⇒ Object
Link this content item to a taxon.
-
#ancestors ⇒ Array
Get ancestors of a taxon.
-
#breadcrumb_trail ⇒ Array
Get a breadcrumb trail for a taxon.
-
#count ⇒ Integer
The number of taxons in this branch of the taxonomy.
-
#depth ⇒ Integer
The number of taxons between this taxon and the taxonomy root.
-
#descendants ⇒ Array
Get descendants of a taxon.
-
#initialize(title:, base_path:, content_id:, internal_name: nil) ⇒ LinkedContentItem
constructor
A new instance of LinkedContentItem.
-
#inspect ⇒ String
The string representation of the content item.
-
#root? ⇒ Boolean
Whether this taxon is the root of its taxonomy.
-
#taxons_with_ancestors ⇒ Array
Get all linked taxons and their ancestors.
-
#tree ⇒ Array
Get taxons in the taxon's branch of the taxonomy.
Constructor Details
#initialize(title:, base_path:, content_id:, internal_name: nil) ⇒ LinkedContentItem
Returns a new instance of LinkedContentItem.
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_path ⇒ Object (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 |
#children ⇒ Object (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_id ⇒ Object (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_name ⇒ Object (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 |
#parent ⇒ Object
Returns the value of attribute parent.
14 15 16 |
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 14 def parent @parent end |
#taxons ⇒ Object (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 |
#title ⇒ Object (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
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.(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
100 101 102 |
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 100 def add_taxon(taxon_node) taxons << taxon_node end |
#ancestors ⇒ Array
Get ancestors of a 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 |
#breadcrumb_trail ⇒ Array
Get a breadcrumb trail for a taxon
70 71 72 |
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 70 def ancestors + [self] end |
#count ⇒ Integer
Returns 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 |
#depth ⇒ Integer
Returns 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 |
#descendants ⇒ Array
Get descendants of a taxon
52 53 54 |
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 52 def descendants tree.tap(&:shift) end |
#inspect ⇒ String
Returns 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.
87 88 89 |
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 87 def root? parent.nil? end |
#taxons_with_ancestors ⇒ Array
Get all linked taxons and their ancestors
77 78 79 |
# File 'lib/govuk_taxonomy_helpers/linked_content_item.rb', line 77 def taxons_with_ancestors taxons.flat_map(&:breadcrumb_trail) end |
#tree ⇒ Array
Get taxons in the taxon's branch of the taxonomy.
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 |