Class: GovukElementsFormBuilder::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- GovukElementsFormBuilder::FormBuilder
- Defined in:
- lib/govuk_elements_form_builder/form_builder.rb
Instance Attribute Summary collapse
-
#current_fieldset_attribute ⇒ Object
Used to propagate the fieldset outer element attribute to the inner elements.
Instance Method Summary collapse
- #check_box_fieldset(legend_key, attributes, options = {}, &block) ⇒ Object
-
#check_box_input(attribute, options = {}, &block) ⇒ Object
The following method will generate revealing panel markup and internally call the ‘check_box_inputs` private method.
- #collection_check_boxes(method, collection, value_method, text_method, options = {}, *args) ⇒ Object
- #collection_radio_buttons(method, collection, value_method, text_method, options = {}, *args) ⇒ Object
- #collection_select(method, collection, value_method, text_method, options = {}, *args) ⇒ Object
-
#fields_for(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object
Ensure fields_for yields a GovukElementsFormBuilder.
- #radio_button_fieldset(attribute, options = {}, &block) ⇒ Object
-
#radio_input(choice, options = {}, &block) ⇒ Object
The following method will generate revealing panel markup and internally call the ‘radio_inputs` private method.
- #revealing_panel(panel_id, options = {}, &block) ⇒ Object
Instance Attribute Details
#current_fieldset_attribute ⇒ Object
Used to propagate the fieldset outer element attribute to the inner elements
12 13 14 |
# File 'lib/govuk_elements_form_builder/form_builder.rb', line 12 def current_fieldset_attribute @current_fieldset_attribute end |
Instance Method Details
#check_box_fieldset(legend_key, attributes, options = {}, &block) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/govuk_elements_form_builder/form_builder.rb', line 60 def check_box_fieldset legend_key, attributes, ={}, &block content_tag :div, class: form_group_classes(attributes), id: form_group_id(attributes) do content_tag :fieldset, (attributes, ) do safe_join([ fieldset_legend(legend_key, ), block_given? ? capture(self, &block) : check_box_inputs(attributes, ) ], "\n") end end end |
#check_box_input(attribute, options = {}, &block) ⇒ Object
The following method will generate revealing panel markup and internally call the ‘check_box_inputs` private method. It is not intended to be used outside a fieldset tag (at the moment, `check_box_fieldset`).
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/govuk_elements_form_builder/form_builder.rb', line 151 def check_box_input attribute, = {}, &block panel = if block_given? || .key?(:panel_id) panel_id = .delete(:panel_id) { [attribute, 'panel'].join('_') } .merge!('data-target': panel_id) revealing_panel(panel_id, flush: false, &block) if block_given? end checkbox = check_box_inputs([attribute], ).first + "\n" safe_concat([checkbox, panel].join) end |
#collection_check_boxes(method, collection, value_method, text_method, options = {}, *args) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/govuk_elements_form_builder/form_builder.rb', line 87 def collection_check_boxes method, collection, value_method, text_method, = {}, *args content_tag :div, class: form_group_classes(method), id: form_group_id(method) do content_tag :fieldset, (method, ) do legend_key = method legend = fieldset_legend(legend_key, ) collection = super(method, collection, value_method, text_method, ) do |b| content_tag :div, class: "multiple-choice" do b.check_box + b.label end end (legend + collection).html_safe end end end |
#collection_radio_buttons(method, collection, value_method, text_method, options = {}, *args) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/govuk_elements_form_builder/form_builder.rb', line 106 def method, collection, value_method, text_method, = {}, *args content_tag :div, class: form_group_classes(method), id: form_group_id(method) do content_tag :fieldset, (method, ) do legend_key = method legend = fieldset_legend(legend_key, ) collection = super(method, collection, value_method, text_method, ) do |b| content_tag :div, class: "multiple-choice" do b. + b.label end end (legend + collection).html_safe end end end |
#collection_select(method, collection, value_method, text_method, options = {}, *args) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/govuk_elements_form_builder/form_builder.rb', line 73 def collection_select method, collection, value_method, text_method, = {}, *args content_tag :div, class: form_group_classes(method), id: form_group_id(method) do = args. set_field_classes! , method label = label(method, class: "form-label") add_hint :label, label, method (label+ super(method, collection, value_method, text_method, , )).html_safe end end |
#fields_for(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object
Ensure fields_for yields a GovukElementsFormBuilder.
15 16 17 |
# File 'lib/govuk_elements_form_builder/form_builder.rb', line 15 def fields_for record_name, record_object = nil, = {}, &block super record_name, record_object, .merge(builder: self.class), &block end |
#radio_button_fieldset(attribute, options = {}, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/govuk_elements_form_builder/form_builder.rb', line 47 def attribute, ={}, &block content_tag :div, class: form_group_classes(attribute), id: form_group_id(attribute) do content_tag :fieldset, (attribute, ) do safe_join([ fieldset_legend(attribute, ), block_given? ? capture(self, &block) : radio_inputs(attribute, ) ], "\n") end end end |
#radio_input(choice, options = {}, &block) ⇒ Object
The following method will generate revealing panel markup and internally call the ‘radio_inputs` private method. It is not intended to be used outside a fieldset tag (at the moment, `radio_button_fieldset`).
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/govuk_elements_form_builder/form_builder.rb', line 130 def radio_input choice, = {}, &block fieldset_attribute = self.current_fieldset_attribute panel = if block_given? || .key?(:panel_id) panel_id = .delete(:panel_id) { [fieldset_attribute, choice, 'panel'].join('_') } .merge!('data-target': panel_id) revealing_panel(panel_id, flush: false, &block) if block_given? end option = radio_inputs( fieldset_attribute, .merge(choices: [choice]) ).first + "\n" safe_concat([option, panel].join) end |
#revealing_panel(panel_id, options = {}, &block) ⇒ Object
163 164 165 166 167 168 169 |
# File 'lib/govuk_elements_form_builder/form_builder.rb', line 163 def revealing_panel panel_id, = {}, &block panel = content_tag( :div, class: 'panel panel-border-narrow js-hidden', id: panel_id ) { block.call(BlockBuffer.new(self)) } + "\n" .fetch(:flush, true) ? safe_concat(panel) : panel end |