Class: GovukPublishingComponents::Presenters::ContentsListHelper

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
lib/govuk_publishing_components/presenters/contents_list_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ContentsListHelper

Returns a new instance of ContentsListHelper.



10
11
12
13
14
# File 'lib/govuk_publishing_components/presenters/contents_list_helper.rb', line 10

def initialize(options)
  @contents = options[:contents] || []
  @nested = @contents.any? { |c| c[:items] && c[:items].any? }
  @format_numbers = options[:format_numbers]
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



8
9
10
# File 'lib/govuk_publishing_components/presenters/contents_list_helper.rb', line 8

def classes
  @classes
end

#contentsObject (readonly)

Returns the value of attribute contents.



8
9
10
# File 'lib/govuk_publishing_components/presenters/contents_list_helper.rb', line 8

def contents
  @contents
end

Instance Method Details

#get_ga4_event_name(link) ⇒ Object



51
52
53
54
55
# File 'lib/govuk_publishing_components/presenters/contents_list_helper.rb', line 51

def get_ga4_event_name(link)
  return "select_content" if link.start_with?("#")

  "navigation"
end

#get_index_totalObject



43
44
45
46
47
48
49
# File 'lib/govuk_publishing_components/presenters/contents_list_helper.rb', line 43

def get_index_total
  total = @contents.length
  @contents.each do |parent|
    total += parent[:items].length if parent[:items]
  end
  total
end

#list_item_classes(list_item, nested) ⇒ Object



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

def list_item_classes(list_item, nested)
  list_item_classes = "gem-c-contents-list__list-item"
  list_item_classes << " gem-c-contents-list__list-item--#{parent_modifier}" unless nested
  list_item_classes << " gem-c-contents-list__list-item--dashed" if nested
  list_item_classes << " gem-c-contents-list__list-item--active" if list_item[:active]

  list_item_classes
end

#wrap_numbers_with_spans(content_item_link) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/govuk_publishing_components/presenters/contents_list_helper.rb', line 25

def wrap_numbers_with_spans(content_item_link)
  content_item_text = strip_tags(content_item_link) # just the text of the link
  content_item_text_stripped = content_item_text.strip # strip trailing spaces for the regex. Keep original content_item_text for the string replacement.
  # Must start with a number
  # Number must be between 1 and 999 (ie not 2014)
  # Must be followed by a space
  # May contain a period `1.`
  # May be a decimal `1.2`
  number = /^\d{1,3}(\.?|\.\d{1,2})(?=\s)/.match(content_item_text_stripped)

  if number
    words = content_item_text.sub(number.to_s, "").strip # remove the number from the text
    content_item_link.sub(content_item_text, "<span class=\"gem-c-contents-list__number\">#{number} </span><span class=\"gem-c-contents-list__numbered-text\">#{words}</span>").squish.html_safe
  else
    content_item_link
  end
end