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
WORLD_LOCATION_SPECIAL_CASES =
{
  "UK Mission to the European Union" => "uk-mission-to-the-eu",
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RelatedNavigationHelper

Returns a new instance of RelatedNavigationHelper.



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

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

Instance Method Details



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

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

#construct_section_heading(section_title) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 56

def construct_section_heading(section_title)
  unless section_title == "related_items"
    I18n.t(
      "components.related_#{@context}_navigation." + section_title,
      default: [
        I18n.t("components.related_navigation.#{section_title}"),
        section_title.tr("_", " "),
      ],
    )
  end
end


24
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
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 24

def related_navigation
  case @context.try(:to_sym)
  when :sidebar
    {
      "related_items" => related_items,
      "related_guides" => related_guides,
      "collections" => related_document_collections,
    }
  when :footer
    {
      "topics" => related_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_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

Returns:

  • (Boolean)


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

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


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

def remaining_link_count(links)
  links.length - MAX_SECTION_LENGTH
end

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



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 68

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



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

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

#section_heading_levelObject



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

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