Class: GovukPublishingComponents::Presenters::RelatedNavigationHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_publishing_components/presenters/related_navigation_helper.rb

Overview

Only used by the related_navigation component

Constant Summary collapse

MAX_SECTION_LENGTH =
5
DEFINED_SECTIONS =
%w[
  related_guides
  topics
  collections
  topical_events
  world_locations
  statistical_data_sets
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RelatedNavigationHelper

Returns a new instance of RelatedNavigationHelper.



18
19
20
21
22
23
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 18

def initialize(options = {})
  @content_item = options.fetch(:content_item) { raise ArgumentError, "missing argument: content_item" }
  @context = options.fetch(:context, nil)
  @related_navigation = related_navigation_contents
  @index_section_count = section_count
end

Instance Attribute Details

#index_section_countObject (readonly)

Returns the value of attribute index_section_count.



16
17
18
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 16

def index_section_count
  @index_section_count
end

Returns the value of attribute related_navigation.



16
17
18
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 16

def related_navigation
  @related_navigation
end

Instance Method Details



102
103
104
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 102

def calculate_section_link_limit(links)
  links.length == MAX_SECTION_LENGTH + 1 ? MAX_SECTION_LENGTH + 1 : MAX_SECTION_LENGTH
end

#construct_ga4_section_text(section) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 73

def construct_ga4_section_text(section)
  # Force English so we can still understand what is being tracked if translated.
  underscores_to_spaces = true
  underscores_to_spaces = false if section == "related_content"
  I18n.with_locale(:en) do
    construct_section_text(section, underscores_to_spaces)
  end
end

#construct_section_text(section, underscores_to_spaces) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 61

def construct_section_text(section, underscores_to_spaces)
  unless section == "related_items"
    defaults = [I18n.t("components.related_navigation.#{section}")]
    defaults << section.tr("_", " ") if underscores_to_spaces

    I18n.t(
      "components.related_#{@context}_navigation.#{section}",
      default: defaults,
    )
  end
end

Returns:

  • (Boolean)


110
111
112
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 110

def related_navigation?
  related_navigation.flat_map(&:last).any?
end


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 25

def related_navigation_contents
  case @context.try(:to_sym)
  when :sidebar
    {
      "related_items" => related_items,
      "related_guides" => related_guides,
      "collections" => related_document_collections,
    }
  when :footer
    {
      "topics" => related_browse_topics_or_taxons,
      "topical_events" => related_topical_events,
      "world_locations" => related_world_locations,
      "statistical_data_sets" => related_statistical_data_sets,
      "related_external_links" => related_external_links,
      "related_contacts" => related_contacts,
    }
  else
    {
      "related_items" => related_items,
      "related_guides" => related_guides,
      "collections" => related_document_collections,
      "topics" => related_browse_topics_or_taxons,
      "topical_events" => related_topical_events,
      "world_locations" => related_world_locations,
      "statistical_data_sets" => related_statistical_data_sets,
      "related_external_links" => related_external_links,
      "related_contacts" => related_contacts,
    }
  end
end


106
107
108
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 106

def remaining_link_count(links)
  links.length - MAX_SECTION_LENGTH
end

#section_countObject



57
58
59
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 57

def section_count
  @related_navigation.select { |_, links| links.any? }.length
end

#section_css_class(css_class, section_title, link: {}, link_is_inline: false) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 82

def section_css_class(css_class, section_title, link: {}, link_is_inline: false)
  css_classes = [css_class]
  css_classes << "#{css_class}--#{@context}" unless @context.nil?
  css_classes << "#{css_class}--inline" if link_is_inline

  unless DEFINED_SECTIONS.include?(section_title) || link.fetch(:finder, false)
    css_classes << " #{css_class}--other"
  end

  css_classes.join(" ")
end

#section_data_track_count(section_title) ⇒ Object



94
95
96
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 94

def section_data_track_count(section_title)
  String(@context || "sidebar") + String(section_title).camelcase
end

#section_heading_levelObject



98
99
100
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 98

def section_heading_level
  @context == :footer ? "h2" : "h3"
end