Class: GovukPublishingComponents::Presenters::RelatedNavigationHelper
- Inherits:
-
Object
- Object
- GovukPublishingComponents::Presenters::RelatedNavigationHelper
- 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
-
#index_section_count ⇒ Object
readonly
Returns the value of attribute index_section_count.
-
#related_navigation ⇒ Object
readonly
Returns the value of attribute related_navigation.
Instance Method Summary collapse
- #calculate_section_link_limit(links) ⇒ Object
- #construct_ga4_section_text(section) ⇒ Object
- #construct_section_text(section, underscores_to_spaces) ⇒ Object
-
#initialize(options = {}) ⇒ RelatedNavigationHelper
constructor
A new instance of RelatedNavigationHelper.
- #related_navigation? ⇒ Boolean
- #related_navigation_contents ⇒ Object
- #remaining_link_count(links) ⇒ Object
- #section_count ⇒ Object
- #section_css_class(css_class, section_title, link: {}, link_is_inline: false) ⇒ Object
- #section_data_track_count(section_title) ⇒ Object
- #section_heading_level ⇒ Object
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( = {}) @content_item = .fetch(:content_item) { raise ArgumentError, "missing argument: content_item" } @context = .fetch(:context, nil) @related_navigation = @index_section_count = section_count end |
Instance Attribute Details
#index_section_count ⇒ Object (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 |
#related_navigation ⇒ Object (readonly)
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 end |
Instance Method Details
#calculate_section_link_limit(links) ⇒ Object
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 |
#related_navigation? ⇒ Boolean
110 111 112 |
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 110 def .flat_map(&:last).any? end |
#related_navigation_contents ⇒ Object
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 case @context.try(:to_sym) when :sidebar { "related_items" => , "related_guides" => , "collections" => , } when :footer { "topics" => , "topical_events" => , "world_locations" => , "statistical_data_sets" => , "related_external_links" => , "related_contacts" => , } else { "related_items" => , "related_guides" => , "collections" => , "topics" => , "topical_events" => , "world_locations" => , "statistical_data_sets" => , "related_external_links" => , "related_contacts" => , } end end |
#remaining_link_count(links) ⇒ Object
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_count ⇒ Object
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_level ⇒ Object
98 99 100 |
# File 'lib/govuk_publishing_components/presenters/related_navigation_helper.rb', line 98 def section_heading_level @context == :footer ? "h2" : "h3" end |