class GovukPublishingComponents::Presenters::ContentsListHelper

Attributes

classes[R]
contents[R]

Public Class Methods

new(options) click to toggle source
# 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

Public Instance Methods

list_item_classes(list_item, nested) click to toggle source
# 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) click to toggle source
# 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

  # 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)

  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

Private Instance Methods

parent_modifier() click to toggle source
# File lib/govuk_publishing_components/presenters/contents_list_helper.rb, line 45
def parent_modifier
  if @nested
    "parent"
  elsif @format_numbers
    "numbered"
  else
    "dashed"
  end
end