Class: GovukNavigationHelpers::ContentItem

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_navigation_helpers/content_item.rb

Overview

Simple wrapper around a content store representation of a content item. Works for both the main content item and the expanded links in the links hash.

Constant Summary collapse

WORLD_TAXON_CONTENT_ID =
"91b8ef20-74e7-4552-880c-50e6d73c2ff9".freeze
EDUCATION_TAXON_CONTENT_ID =
"c58fdadd-7743-46d6-9629-90bb3ccc4ef0".freeze
CHILDCARE_PARENTING_TAXON_CONTENT_ID =
"206b7f3a-49b5-476f-af0f-fd27e2a68473".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_store_response) ⇒ ContentItem

Returns a new instance of ContentItem.



22
23
24
# File 'lib/govuk_navigation_helpers/content_item.rb', line 22

def initialize(content_store_response)
  @content_store_response = content_store_response.to_h
end

Instance Attribute Details

#content_store_responseObject (readonly)

Returns the value of attribute content_store_response.



20
21
22
# File 'lib/govuk_navigation_helpers/content_item.rb', line 20

def content_store_response
  @content_store_response
end

Class Method Details

.whitelisted_root_taxon_content_idsObject



12
13
14
15
16
17
18
# File 'lib/govuk_navigation_helpers/content_item.rb', line 12

def self.whitelisted_root_taxon_content_ids
  [
    WORLD_TAXON_CONTENT_ID,
    EDUCATION_TAXON_CONTENT_ID,
    CHILDCARE_PARENTING_TAXON_CONTENT_ID,
  ]
end

Instance Method Details

#==(other) ⇒ Object



135
136
137
# File 'lib/govuk_navigation_helpers/content_item.rb', line 135

def ==(other)
  content_id == other.content_id
end


128
129
130
131
132
133
# File 'lib/govuk_navigation_helpers/content_item.rb', line 128

def as_taxonomy_sidebar_link
  {
    title: title,
    link: base_path,
  }
end

#base_pathObject



56
57
58
# File 'lib/govuk_navigation_helpers/content_item.rb', line 56

def base_path
  content_store_response.fetch("base_path")
end

#content_idObject



64
65
66
# File 'lib/govuk_navigation_helpers/content_item.rb', line 64

def content_id
  content_store_response.fetch("content_id")
end


74
75
76
77
78
# File 'lib/govuk_navigation_helpers/content_item.rb', line 74

def curated_taxonomy_sidebar_links
  content_store_response.dig("links", "ordered_related_items_overrides").to_a.map do |link|
    ContentItem.new(link)
  end
end

#descriptionObject



60
61
62
# File 'lib/govuk_navigation_helpers/content_item.rb', line 60

def description
  content_store_response.fetch("description", "")
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/govuk_navigation_helpers/content_item.rb', line 143

def eql?(other)
  self == other
end


124
125
126
# File 'lib/govuk_navigation_helpers/content_item.rb', line 124

def external_links
  content_store_response.dig("details", "external_related_links").to_a
end

#hashObject



139
140
141
# File 'lib/govuk_navigation_helpers/content_item.rb', line 139

def hash
  content_id.hash
end

#mainstream_browse_pagesObject



46
47
48
49
50
# File 'lib/govuk_navigation_helpers/content_item.rb', line 46

def mainstream_browse_pages
  content_store_response.dig("links", "mainstream_browse_pages").to_a.map do |link|
    ContentItem.new(link)
  end
end

#parentObject



26
27
28
29
30
# File 'lib/govuk_navigation_helpers/content_item.rb', line 26

def parent
  parent_item = content_store_response.dig("links", "parent", 0)
  return unless parent_item
  ContentItem.new(parent_item)
end

#parent_taxonObject



32
33
34
35
36
# File 'lib/govuk_navigation_helpers/content_item.rb', line 32

def parent_taxon
  # TODO: Determine what to do when there are multiple taxons/parents. For
  # now just display the first of each.
  parent_taxons.sort_by(&:title).first
end

#parent_taxonsObject



38
39
40
41
42
43
44
# File 'lib/govuk_navigation_helpers/content_item.rb', line 38

def parent_taxons
  @_parent_taxons ||= begin
    taxon_links
      .select { |t| descendent_from_whitelisted_root_taxon?(t) }
      .map { |taxon| ContentItem.new(taxon) }.sort_by(&:title)
  end
end


84
85
86
# File 'lib/govuk_navigation_helpers/content_item.rb', line 84

def quick_links
  content_store_response.dig("details", "quick_links").to_a
end


88
89
90
# File 'lib/govuk_navigation_helpers/content_item.rb', line 88

def related_collections
  filter_link_type(content_store_response.dig("links", "document_collections").to_a, "document_collection")
end


68
69
70
71
72
# File 'lib/govuk_navigation_helpers/content_item.rb', line 68

def related_links
  content_store_response.dig("links", "ordered_related_items").to_a.map do |link|
    ContentItem.new(link)
  end
end


80
81
82
# File 'lib/govuk_navigation_helpers/content_item.rb', line 80

def related_ordered_items
  content_store_response.dig("links", "ordered_related_items").to_a
end


96
97
98
# File 'lib/govuk_navigation_helpers/content_item.rb', line 96

def related_organisations
  filter_link_type(content_store_response.dig("links", "organisations").to_a, "organisation")
end


92
93
94
# File 'lib/govuk_navigation_helpers/content_item.rb', line 92

def related_other_contacts
  filter_link_type(content_store_response.dig("links", "related").to_a, "contact")
end


100
101
102
# File 'lib/govuk_navigation_helpers/content_item.rb', line 100

def related_policies
  filter_link_type(content_store_response.dig("links", "related_policies").to_a, "policy")
end


104
105
106
# File 'lib/govuk_navigation_helpers/content_item.rb', line 104

def related_statistical_data_sets
  filter_link_type(content_store_response.dig("links", "related_statistical_data_sets").to_a, "statistical_data_set")
end


112
113
114
# File 'lib/govuk_navigation_helpers/content_item.rb', line 112

def related_topical_events
  filter_link_type(content_store_response.dig("links", "topical_events").to_a, "topical_event")
end


108
109
110
# File 'lib/govuk_navigation_helpers/content_item.rb', line 108

def related_topics
  filter_link_type(content_store_response.dig("links", "topics").to_a, "topic")
end


116
117
118
# File 'lib/govuk_navigation_helpers/content_item.rb', line 116

def related_world_locations
  content_store_response.dig("links", "world_locations").to_a
end


120
121
122
# File 'lib/govuk_navigation_helpers/content_item.rb', line 120

def related_worldwide_organisations
  filter_link_type(content_store_response.dig("links", "worldwide_organisations").to_a, "worldwide_organisation")
end

#titleObject



52
53
54
# File 'lib/govuk_navigation_helpers/content_item.rb', line 52

def title
  content_store_response.fetch("title")
end