Class: GovukPublishingComponents::Presenters::CheckboxesHelper

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers
Defined in:
lib/govuk_publishing_components/presenters/checkboxes_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CheckboxesHelper

Returns a new instance of CheckboxesHelper.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 18

def initialize(options)
  @items = options[:items] || []
  @name = options[:name]
  @css_classes = %w[gem-c-checkboxes govuk-form-group]
  @css_classes << "govuk-form-group--error" if options[:error]
  @css_classes << "govuk-checkboxes--small" if options[:small]
  @error = true if options[:error]

  # check if any item is set as being conditional
  @has_conditional = options[:items].any? { |item| item.is_a?(Hash) && item[:conditional] }
  @has_nested = options[:items].any? { |item| item.is_a?(Hash) && item[:items] }

  @id = options[:id] || "checkboxes-#{SecureRandom.hex(4)}"
  @heading = options[:heading] || nil
  @heading_caption = options[:heading_caption] || nil
  @heading_size = options[:heading_size] || nil
  @is_page_heading = options[:is_page_heading]
  @description = options[:description] || nil
  @no_hint_text = options[:no_hint_text]
  @hint_text = options[:hint_text] || "Select all that apply." unless @no_hint_text
  @visually_hide_heading = options[:visually_hide_heading]
end

Instance Attribute Details

#css_classesObject (readonly)

Returns the value of attribute css_classes.



7
8
9
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 7

def css_classes
  @css_classes
end

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 7

def description
  @description
end

#errorObject (readonly)

Returns the value of attribute error.



7
8
9
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 7

def error
  @error
end

#has_conditionalObject (readonly)

Returns the value of attribute has_conditional.



7
8
9
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 7

def has_conditional
  @has_conditional
end

#has_nestedObject (readonly)

Returns the value of attribute has_nested.



7
8
9
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 7

def has_nested
  @has_nested
end

#heading_captionObject (readonly)

Returns the value of attribute heading_caption.



7
8
9
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 7

def heading_caption
  @heading_caption
end

#hint_textObject (readonly)

Returns the value of attribute hint_text.



7
8
9
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 7

def hint_text
  @hint_text
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 7

def id
  @id
end

#itemsObject (readonly)

Returns the value of attribute items.



7
8
9
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 7

def items
  @items
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 7

def name
  @name
end

Instance Method Details

#checkbox_markup(checkbox, index) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 86

def checkbox_markup(checkbox, index)
  checkbox_id = checkbox[:id] || "#{@id}-#{index}"
  controls = checkbox[:controls]
  aria_controls = "#{checkbox_id}-conditional-#{index || rand(1..100)}" if checkbox[:conditional].present?
  checkbox_name = checkbox[:name].present? ? checkbox[:name] : @name
  checked = true if checkbox[:checked].present?
  data = checkbox[:data_attributes] || {}
  data[:controls] = controls
  data["aria-controls"] = aria_controls
  data[:behaviour] = "exclusive" if checkbox[:exclusive]

  capture do
    concat check_box_tag(checkbox_name, checkbox[:value], checked, class: "govuk-checkboxes__input", id: checkbox_id, data:)
    concat (:label, checkbox[:label], for: checkbox_id, class: "govuk-label govuk-checkboxes__label")
    concat (:div, checkbox[:hint], id: "#{checkbox_id}-item-hint", class: "govuk-hint govuk-checkboxes__hint") if checkbox[:hint]
  end
end

#fieldset_describedbyObject



47
48
49
50
51
52
53
54
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 47

def fieldset_describedby
  unless @no_hint_text
    text = %w[]
    text << "#{id}-hint" if @hint_text
    text << "#{id}-error" if @error
    text
  end
end

#heading_markupObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 66

def heading_markup
  return unless @heading.present?

  if @is_page_heading
    (
      :legend,
      class: "govuk-fieldset__legend govuk-fieldset__legend--#{size}",
    ) do
      concat (:span, heading_caption, class: "govuk-caption-#{size}") if heading_caption.present?
      concat (:h1, @heading, class: "gem-c-checkboxes__heading-text govuk-fieldset__heading")
    end
  else
    classes = %w[govuk-fieldset__legend]
    classes << "govuk-fieldset__legend--#{size}"
    classes << "gem-c-checkboxes__legend--hidden" if @visually_hide_heading

    (:legend, @heading, class: classes)
  end
end

#should_have_fieldsetObject

should have a fieldset if there’s a heading, or if more than one checkbox separate check is in the view for if more than one checkbox and no heading, in which case fail



43
44
45
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 43

def should_have_fieldset
  @items.length > 1 || @heading
end

#sizeObject



56
57
58
59
60
61
62
63
64
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 56

def size
  if %w[s m l xl].include?(@heading_size)
    @heading_size
  elsif @is_page_heading
    "xl"
  else
    "m"
  end
end