Class: GovukPublishingComponents::Presenters::SharedHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_publishing_components/presenters/shared_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_assigns) ⇒ SharedHelper

Returns a new instance of SharedHelper.



6
7
8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



4
5
6
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 4

def classes
  @classes
end

#heading_levelObject (readonly)

Returns the value of attribute heading_level.



4
5
6
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 4

def heading_level
  @heading_level
end

#margin_bottomObject (readonly)

Returns the value of attribute margin_bottom.



4
5
6
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 4

def margin_bottom
  @margin_bottom
end

#margin_topObject (readonly)

Returns the value of attribute margin_top.



4
5
6
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 4

def margin_top
  @margin_top
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 4

def options
  @options
end

Instance Method Details

#get_heading_levelObject



28
29
30
31
32
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
# 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_bottomObject



24
25
26
# 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_topObject



20
21
22
# 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 = {}) ⇒ Object



70
71
72
73
# 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 = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



75
76
77
# 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) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 34

def valid_heading_size?(size)
  %w[xl l m s].include?(size)
end