Class: GovukNavigationHelpers::GroupedRelatedLinks

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

Overview

Take a content item and group the related links according to an algorithm that is intended to display the related links into three groups, depending on how much they have in common with the main content item.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_item) ⇒ GroupedRelatedLinks

Returns a new instance of GroupedRelatedLinks.



10
11
12
# File 'lib/govuk_navigation_helpers/grouped_related_links.rb', line 10

def initialize(content_item)
  @content_item = content_item
end

Instance Attribute Details

#content_itemObject (readonly)

Returns the value of attribute content_item.



8
9
10
# File 'lib/govuk_navigation_helpers/grouped_related_links.rb', line 8

def content_item
  @content_item
end

Instance Method Details

#parents_tagged_to_same_mainstream_browse_pageObject

This will return related items whose parents are tagged to the same mainstream browse page as the main content item’s parent.



26
27
28
29
30
31
32
33
34
35
# File 'lib/govuk_navigation_helpers/grouped_related_links.rb', line 26

def parents_tagged_to_same_mainstream_browse_page
  return [] unless content_item.parent && content_item.parent.parent

  common_parent_content_ids = tagged_to_same_mainstream_browse_page.map(&:content_id)

  @parents_tagged_to_same_mainstream_browse_page ||= content_item.related_links.select do |related_item|
    next if common_parent_content_ids.include?(related_item.content_id)
    related_item.mainstream_browse_pages.map(&:parent).map(&:content_id).include?(content_item.parent.parent.content_id)
  end
end

#tagged_to_different_mainstream_browse_pagesObject

This will return related links that are tagged to mainstream browse pages unrelated to the main content item.



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

def tagged_to_different_mainstream_browse_pages
  all_content_ids = (tagged_to_same_mainstream_browse_page + parents_tagged_to_same_mainstream_browse_page).map(&:content_id)

  @tagged_to_different_mainstream_browse_pages ||= content_item.related_links.reject do |related_item|
    all_content_ids.include?(related_item.content_id)
  end
end

#tagged_to_same_mainstream_browse_pageObject

This will return related items that are tagged to the same mainstream browse page as the main content item.



16
17
18
19
20
21
22
# File 'lib/govuk_navigation_helpers/grouped_related_links.rb', line 16

def tagged_to_same_mainstream_browse_page
  return [] unless content_item.parent

  @tagged_to_same_mainstream_browse_page ||= content_item.related_links.select do |related_item|
    related_item.mainstream_browse_pages.map(&:content_id).include?(content_item.parent.content_id)
  end
end