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
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 6

def initialize(local_assigns)
  @options = local_assigns
  @margin_bottom = @options[:margin_bottom] || 3
  @heading_level = @options[:heading_level] || 2
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

#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



12
13
14
15
16
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 12

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



22
23
24
25
26
27
28
29
30
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 22

def get_heading_size(option, fallback)
  govuk_class = "govuk-heading-"

  if valid_heading_size?(option)
    "#{govuk_class}#{option}"
  else
    "#{govuk_class}#{fallback}"
  end
end

#t_lang(content, options = {}) ⇒ Object



54
55
56
57
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 54

def t_lang(content, options = {})
  locale = t_locale(content, options)
  "lang=#{locale}" unless locale.eql?(I18n.locale)
end

#t_locale(content, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 32

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



59
60
61
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 59

def t_locale_check(locale)
  locale.presence unless locale.to_s.eql?(I18n.locale.to_s)
end

#valid_heading_size?(size) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/govuk_publishing_components/presenters/shared_helper.rb', line 18

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