Class: GovukPublishingComponents::Presenters::CheckboxesHelper
- Inherits:
-
Object
- Object
- GovukPublishingComponents::Presenters::CheckboxesHelper
- Includes:
- ActionView::Context, ActionView::Helpers
- Defined in:
- lib/govuk_publishing_components/presenters/checkboxes_helper.rb
Instance Attribute Summary collapse
-
#css_classes ⇒ Object
readonly
Returns the value of attribute css_classes.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#has_conditional ⇒ Object
readonly
Returns the value of attribute has_conditional.
-
#has_nested ⇒ Object
readonly
Returns the value of attribute has_nested.
-
#heading_caption ⇒ Object
readonly
Returns the value of attribute heading_caption.
-
#hint_text ⇒ Object
readonly
Returns the value of attribute hint_text.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#list_classes ⇒ Object
readonly
Returns the value of attribute list_classes.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #checkbox_markup(checkbox, index) ⇒ Object
- #fieldset_describedby ⇒ Object
- #heading_markup ⇒ Object
-
#initialize(options) ⇒ CheckboxesHelper
constructor
A new instance of CheckboxesHelper.
-
#should_have_fieldset ⇒ Object
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.
- #size ⇒ Object
Constructor Details
#initialize(options) ⇒ CheckboxesHelper
Returns a new instance of CheckboxesHelper.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 19 def initialize() @items = [:items] || [] @name = [:name] @css_classes = %w[gem-c-checkboxes govuk-form-group] @css_classes << "govuk-form-group--error" if [:error] @css_classes << "govuk-checkboxes--small" if [:small] @error = true if [:error] @list_classes = %w[govuk-checkboxes gem-c-checkboxes__list] # check if any item is set as being conditional @has_conditional = [:items].any? { |item| item.is_a?(Hash) && item[:conditional] } @has_nested = [:items].any? { |item| item.is_a?(Hash) && item[:items] } @id = [:id] || "checkboxes-#{SecureRandom.hex(4)}" @heading = [:heading] || nil @heading_caption = [:heading_caption] || nil @heading_size = [:heading_size] || nil @is_page_heading = [:is_page_heading] @description = [:description] || nil @no_hint_text = [:no_hint_text] @hint_text = [:hint_text] || "Select all that apply." unless @no_hint_text @visually_hide_heading = [:visually_hide_heading] end |
Instance Attribute Details
#css_classes ⇒ Object (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 |
#description ⇒ Object (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 |
#error ⇒ Object (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_conditional ⇒ Object (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_nested ⇒ Object (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_caption ⇒ Object (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 end |
#hint_text ⇒ Object (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 |
#id ⇒ Object (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 |
#items ⇒ Object (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 |
#list_classes ⇒ Object (readonly)
Returns the value of attribute list_classes.
7 8 9 |
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 7 def list_classes @list_classes end |
#name ⇒ Object (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
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 89 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 content_tag(:label, checkbox[:label], for: checkbox_id, class: "govuk-label govuk-checkboxes__label") concat content_tag(:span, checkbox[:hint], id: "#{checkbox_id}-item-hint", class: "govuk-hint govuk-checkboxes__hint") if checkbox[:hint] end end |
#fieldset_describedby ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 50 def fieldset_describedby unless @no_hint_text text = %w[] text << "#{id}-hint" if @hint_text text << "#{id}-error" if @error text end end |
#heading_markup ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 69 def heading_markup return unless @heading.present? if @is_page_heading content_tag( :legend, class: "govuk-fieldset__legend govuk-fieldset__legend--#{size}", ) do concat content_tag(:span, , class: "govuk-caption-#{size}") if .present? concat content_tag(: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 content_tag(:legend, @heading, class: classes) end end |
#should_have_fieldset ⇒ Object
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
46 47 48 |
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 46 def should_have_fieldset @items.length > 1 || @heading end |
#size ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/govuk_publishing_components/presenters/checkboxes_helper.rb', line 59 def size if %w[s m l xl].include?(@heading_size) @heading_size elsif @is_page_heading "xl" else "m" end end |