class GovukPublishingComponents::Presenters::SharedHelper

Attributes

classes[R]
heading_level[R]
margin_bottom[R]
margin_top[R]
options[R]

Public Class Methods

new(local_assigns) click to toggle source
# File lib/govuk_publishing_components/presenters/shared_helper.rb, line 6
def initialize(local_assigns)
  @options = local_assigns
  @margin_top = @options[:margin_top] || nil
  @margin_bottom = @options[:margin_bottom] || 3
  @heading_level = @options[:heading_level] || 2

  if local_assigns.include?(:classes)
    @classes = local_assigns[:classes].split(" ")
    unless @classes.all? { |c| c.start_with?("js-") }
      raise(ArgumentError, "Passed classes must be prefixed with `js-`")
    end
  end
end

Public Instance Methods

get_heading_level() click to toggle source
# File lib/govuk_publishing_components/presenters/shared_helper.rb, line 28
def get_heading_level
  return [*1..6].include?(@heading_level) ? "h#{@heading_level}" : "h2" unless @heading_level.zero?

  "span"
end
get_heading_size(option, fallback) click to toggle source
# File lib/govuk_publishing_components/presenters/shared_helper.rb, line 38
def get_heading_size(option, fallback)
  govuk_class = "govuk-heading-"

  if valid_heading_size?(option)
    "#{govuk_class}#{option}"
  else
    "#{govuk_class}#{fallback}"
  end
end
get_margin_bottom() click to toggle source
# File lib/govuk_publishing_components/presenters/shared_helper.rb, line 24
def get_margin_bottom
  [*0..9].include?(@margin_bottom) ? "govuk-!-margin-bottom-#{margin_bottom}" : "govuk-!-margin-bottom-3"
end
get_margin_top() click to toggle source
# File lib/govuk_publishing_components/presenters/shared_helper.rb, line 20
def get_margin_top
  [*0..9].include?(@margin_top) ? "govuk-!-margin-top-#{margin_top}" : ""
end
t_lang(content, options = {}) click to toggle source
# File lib/govuk_publishing_components/presenters/shared_helper.rb, line 70
def t_lang(content, options = {})
  locale = t_locale(content, options)
  "lang=#{locale}" unless locale.eql?(I18n.locale)
end
t_locale(content, options = {}) click to toggle source
# File lib/govuk_publishing_components/presenters/shared_helper.rb, line 48
def t_locale(content, options = {})
  # Check if the content string has a translation
  content_translation_available = translation_present?(content)

  # True, return locale
  this_locale = I18n.locale if content_translation_available
  # If false, return default locale
  this_locale = I18n.default_locale unless content_translation_available

  # Check if default string passed in
  if options[:default].present?
    # Check if the default string has a translation
    default_translation_available = translation_present?(options[:default])
    # If true, return locale
    this_locale = I18n.locale if default_translation_available
    # If false, return default_locale
    this_locale = I18n.default_locale unless default_translation_available
  end

  this_locale
end
t_locale_check(locale) click to toggle source
# File lib/govuk_publishing_components/presenters/shared_helper.rb, line 75
def t_locale_check(locale)
  locale.presence unless locale.to_s.eql?(I18n.locale.to_s)
end
valid_heading_size?(size) click to toggle source
# File lib/govuk_publishing_components/presenters/shared_helper.rb, line 34
def valid_heading_size?(size)
  %w[xl l m s].include?(size)
end

Private Instance Methods

translation_present?(content) click to toggle source
# File lib/govuk_publishing_components/presenters/shared_helper.rb, line 81
def translation_present?(content)
  default_string = "This is a string to act as a default for the `I18n.translate` method. Comparing the result reveals if there is a translation in the i18n files."
  I18n.translate(
    content,
    default: default_string,
    fallback: false,
  ) != default_string
end