Class: GovukPublishingComponents::Presenters::ContentItem

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_publishing_components/presenters/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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_store_response) ⇒ ContentItem

Returns a new instance of ContentItem.



11
12
13
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 11

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.



9
10
11
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 9

def content_store_response
  @content_store_response
end

Instance Method Details

#==(other) ⇒ Object



123
124
125
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 123

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


116
117
118
119
120
121
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 116

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

#base_pathObject



48
49
50
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 48

def base_path
  content_store_response.fetch("base_path")
end

#content_idObject



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

def content_id
  content_store_response.fetch("content_id")
end


70
71
72
73
74
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 70

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



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 131

def eql?(other)
  self == other
end


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

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

#hashObject



127
128
129
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 127

def hash
  content_id.hash
end

#mainstream_browse_pagesObject



38
39
40
41
42
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 38

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

#parentObject



15
16
17
18
19
20
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 15

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

  ContentItem.new(parent_item)
end

#parent_taxonObject



22
23
24
25
26
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 22

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

#parent_taxonsObject



28
29
30
31
32
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 28

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

#phase_is_live?(taxon) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 34

def phase_is_live?(taxon)
  taxon["phase"] == "live"
end


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

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


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

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


64
65
66
67
68
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 64

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


76
77
78
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 76

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


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

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


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

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


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

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


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

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


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

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


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

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

#titleObject



44
45
46
# File 'lib/govuk_publishing_components/presenters/content_item.rb', line 44

def title
  content_store_response.fetch("title")
end

#url_overrideObject



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

def url_override
  content_store_response.dig("details", "url_override")
end