Class: GovukPublishingComponents::Presenters::ContentsListHelper
- Inherits:
-
Object
- Object
- GovukPublishingComponents::Presenters::ContentsListHelper
- Includes:
- ActionView::Helpers::SanitizeHelper
- Defined in:
- lib/govuk_publishing_components/presenters/contents_list_helper.rb
Instance Attribute Summary collapse
-
#classes ⇒ Object
readonly
Returns the value of attribute classes.
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
Instance Method Summary collapse
- #clean_string(text) ⇒ Object
- #get_ga4_event_name(link) ⇒ Object
- #get_index_total ⇒ Object
-
#initialize(options) ⇒ ContentsListHelper
constructor
A new instance of ContentsListHelper.
- #list_item_classes(list_item, nested) ⇒ Object
- #wrap_numbers_with_spans(link_text) ⇒ Object
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() @contents = [:contents] || [] @nested = @contents.any? { |c| c[:items] && c[:items].any? } @format_numbers = [:format_numbers] end |
Instance Attribute Details
#classes ⇒ Object (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 |
#contents ⇒ Object (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
#clean_string(text) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/govuk_publishing_components/presenters/contents_list_helper.rb', line 43 def clean_string(text) text = text.gsub(/ /, " ") text = text.gsub("/\u00a0/", " ") text = text.gsub(160.chr("UTF-8"), " ") text = text.strip (text.tr("\n", "")) end |
#get_ga4_event_name(link) ⇒ Object
59 60 61 62 63 |
# File 'lib/govuk_publishing_components/presenters/contents_list_helper.rb', line 59 def get_ga4_event_name(link) return "select_content" if link.start_with?("#") "navigation" end |
#get_index_total ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/govuk_publishing_components/presenters/contents_list_helper.rb', line 51 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(link_text) ⇒ 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(link_text) output = clean_string(link_text) # 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(output) if number words = output.sub(number.to_s, "").strip # remove the number from the text # the space in the first span is needed for screen readers "<span class=\"gem-c-contents-list__number\">#{number} </span><span class=\"gem-c-contents-list__numbered-text\">#{words}</span>".squish.html_safe else output end end |