Class: GovukElementsFormBuilder::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/govuk_elements_form_builder/form_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_fieldset_attributeObject

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, options={}, &block
   :div,
              class: form_group_classes(attributes),
              id: form_group_id(attributes) do
     :fieldset, fieldset_options(attributes, options) do
      safe_join([
                  fieldset_legend(legend_key, options),
                  block_given? ? capture(self, &block) : check_box_inputs(attributes, options)
                ], "\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, options = {}, &block
  panel = if block_given? || options.key?(:panel_id)
            panel_id = options.delete(:panel_id) { [attribute, 'panel'].join('_') }
            options.merge!('data-target': panel_id)
            revealing_panel(panel_id, flush: false, &block) if block_given?
          end

  checkbox = check_box_inputs([attribute], options).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, options = {}, *args
   :div,
              class: form_group_classes(method),
              id: form_group_id(method) do
     :fieldset, fieldset_options(method, options) do
      legend_key = method
      legend = fieldset_legend(legend_key, options)

      collection =  super(method, collection, value_method, text_method, options) do |b|
                       :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 collection_radio_buttons method, collection, value_method, text_method, options = {}, *args
   :div,
              class: form_group_classes(method),
              id: form_group_id(method) do
     :fieldset, fieldset_options(method, options) do

      legend_key = method
      legend = fieldset_legend(legend_key, options)

      collection =  super(method, collection, value_method, text_method, options) do |b|
                       :div, class: "multiple-choice" do
                        b.radio_button + 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, options = {}, *args

   :div, class: form_group_classes(method), id: form_group_id(method) do

    html_options = args.extract_options!
    set_field_classes! html_options, method

    label = label(method, class: "form-label")
    add_hint :label, label, method

    (label+ super(method, collection, value_method, text_method, options , html_options)).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, fields_options = {}, &block
  super record_name, record_object, fields_options.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 radio_button_fieldset attribute, options={}, &block
   :div,
              class: form_group_classes(attribute),
              id: form_group_id(attribute) do
     :fieldset, fieldset_options(attribute, options) do
      safe_join([
                  fieldset_legend(attribute, options),
                  block_given? ? capture(self, &block) : radio_inputs(attribute, options)
                ], "\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, options = {}, &block
  fieldset_attribute = self.current_fieldset_attribute

  panel = if block_given? || options.key?(:panel_id)
    panel_id = options.delete(:panel_id) { [fieldset_attribute, choice, 'panel'].join('_') }
    options.merge!('data-target': panel_id)
    revealing_panel(panel_id, flush: false, &block) if block_given?
  end

  option = radio_inputs(
    fieldset_attribute,
    options.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, options = {}, &block
  panel = (
    :div, class: 'panel panel-border-narrow js-hidden', id: panel_id
  ) { block.call(BlockBuffer.new(self)) } + "\n"

  options.fetch(:flush, true) ? safe_concat(panel) : panel
end