Class: ComfyBootstrapForm::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- ComfyBootstrapForm::FormBuilder
- Defined in:
- lib/comfy_bootstrap_form/form_builder.rb
Constant Summary collapse
- FIELD_HELPERS =
%w[ color_field date_field datetime_field email_field month_field password_field phone_field range_field search_field text_area text_field rich_text_area time_field url_field week_field ].freeze
- DATE_SELECT_HELPERS =
%w[ date_select datetime_select time_select ].freeze
Instance Attribute Summary collapse
-
#form_bootstrap ⇒ Object
Bootstrap settings set on the form itself.
Instance Method Summary collapse
-
#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
Wrapper around checkbox.
-
#collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
Helper to generate multiple checkboxes.
-
#collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
Helper to generate multiple radio buttons.
-
#file_field(method, options = {}) ⇒ Object
Wrapper for file_field helper.
-
#form_group(options = {}) ⇒ Object
Helper method to put arbitrary content in markup that renders correctly for the Bootstrap form.
-
#initialize(object_name, object, template, options) ⇒ FormBuilder
constructor
A new instance of FormBuilder.
-
#number_field(method, options = {}) ⇒ Object
Wrapper for the number field.
-
#plaintext(method, options = {}) ⇒ Object
Bootstrap wrapper for readonly text field that is shown as plain text.
-
#primary(value = nil, options = {}, &block) ⇒ Object
Same as submit button, only with btn-primary class added.
-
#radio_button(method, tag_value, options = {}) ⇒ Object
Wrapper around radio button.
-
#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object
Wrapper for select helper.
-
#submit(value = nil, options = {}, &block) ⇒ Object
Add bootstrap formatted submit button.
Constructor Details
#initialize(object_name, object, template, options) ⇒ FormBuilder
Returns a new instance of FormBuilder.
23 24 25 26 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 23 def initialize(object_name, object, template, ) @form_bootstrap = ComfyBootstrapForm::BootstrapOptions.new(.delete(:bootstrap)) super(object_name, object, template, ) end |
Instance Attribute Details
#form_bootstrap ⇒ Object
Bootstrap settings set on the form itself
21 22 23 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 21 def form_bootstrap @form_bootstrap end |
Instance Method Details
#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
Wrapper around checkbox. Example usage:
checkbox :agree, bootstrap: {label: {text: "Do you agree?"}}
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 176 def check_box(method, = {}, checked_value = "1", unchecked_value = "0") bootstrap = form_bootstrap.scoped(.delete(:bootstrap)) return super if bootstrap.disabled help_text = draw_help(bootstrap) errors = draw_errors(bootstrap, method) add_css_class!(, "form-check-input") add_css_class!(, "is-invalid") if errors.present? label_text = nil if (custom_text = bootstrap.label[:text]).present? label_text = custom_text end fieldset_css_class = "form-group" fieldset_css_class += " row" if bootstrap.horizontal? fieldset_css_class += " #{bootstrap.inline_margin_class}" if bootstrap.inline? content_tag(:fieldset, class: fieldset_css_class) do draw_control_column(bootstrap, offset: true) do if bootstrap.custom_control content_tag(:div, class: "custom-control custom-checkbox") do add_css_class!(, "custom-control-input") remove_css_class!(, "form-check-input") concat super(method, , checked_value, unchecked_value) concat label(method, label_text, class: "custom-control-label") concat errors if errors.present? concat help_text if help_text.present? end else content_tag(:div, class: "form-check") do concat super(method, , checked_value, unchecked_value) concat label(method, label_text, class: "form-check-label") concat errors if errors.present? concat help_text if help_text.present? end end end end end |
#collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
Helper to generate multiple checkboxes. Same options as for radio buttons. Example usage:
collection_check_boxes :choices, Choice.all, :id, :label
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 244 def collection_check_boxes(method, collection, value_method, text_method, = {}, = {}) bootstrap = form_bootstrap.scoped(.delete(:bootstrap)) return super if bootstrap.disabled content = "".html_safe unless [:include_hidden] == false content << hidden_field(method, multiple: true, value: "") end args = [bootstrap, :check_box, method, collection, value_method, text_method, , ] content << draw_choices(*args) do |m, v, opts| opts[:multiple] = true opts[:include_hidden] = false check_box(m, opts.merge(bootstrap: { disabled: true }), v) end end |
#collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
Helper to generate multiple radio buttons. Example usage:
collection_radio_buttons :choices, ["a", "b"], :to_s, :to_s %>
collection_radio_buttons :choices, [["a", "Label A"], ["b", "Label B"]], :first, :second
collection_radio_buttons :choices, Choice.all, :id, :label
Takes bootstrap options:
inline: true - to render inputs inline
label: {text: "Custom"} - to specify a label
label: {hide: true} - to not render label at all
229 230 231 232 233 234 235 236 237 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 229 def (method, collection, value_method, text_method, = {}, = {}) bootstrap = form_bootstrap.scoped(.delete(:bootstrap)) return super if bootstrap.disabled args = [bootstrap, :radio_button, method, collection, value_method, text_method, , ] draw_choices(*args) do |m, v, opts| (m, v, opts.merge(bootstrap: { disabled: true })) end end |
#file_field(method, options = {}) ⇒ Object
Wrapper for file_field helper. It can accept ‘custom_control` option.
file_field :photo, bootstrap: {custom_control: true}
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 104 def file_field(method, = {}) bootstrap = form_bootstrap.scoped(.delete(:bootstrap)) return super if bootstrap.disabled draw_form_group(bootstrap, method, ) do if bootstrap.custom_control content_tag(:div, class: "custom-file") do add_css_class!(, "custom-file-input") remove_css_class!(, "form-control") label_text = .delete(:placeholder) concat super(method, ) = { class: "custom-file-label" } [:for] = [:id] if [:id].present? concat label(method, label_text, ) end else super(method, ) end end end |
#form_group(options = {}) ⇒ Object
Helper method to put arbitrary content in markup that renders correctly for the Bootstrap form. Example:
form_group bootstrap: {label: {text: "Label"}} do
"Some content"
end
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 323 def form_group( = {}) bootstrap = form_bootstrap.scoped(.delete(:bootstrap)) = bootstrap.label.clone label_text = .delete(:text) label = if label_text.present? if bootstrap.horizontal? add_css_class!(, "col-form-label") add_css_class!(, bootstrap.label_col_class) add_css_class!(, bootstrap.label_align_class) elsif bootstrap.inline? add_css_class!(, bootstrap.inline_margin_class) end content_tag(:label, label_text, ) end form_group_class = "form-group" form_group_class += " row" if bootstrap.horizontal? form_group_class += " mr-sm-2" if bootstrap.inline? content_tag(:div, class: form_group_class) do content = "".html_safe content << label if label.present? content << draw_control_column(bootstrap, offset: label.blank?) do yield end end end |
#number_field(method, options = {}) ⇒ Object
Wrapper for the number field. It has default changed from ‘step: “1”` to `step: “any”` to prevent confusion when dealing with decimal numbers.
number_field :amount, step: 5
75 76 77 78 79 80 81 82 83 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 75 def number_field(method, = {}) bootstrap = form_bootstrap.scoped(.delete(:bootstrap)) .reverse_merge!(step: "any") return super(method, ) if bootstrap.disabled draw_form_group(bootstrap, method, ) do super(method, ) end end |
#plaintext(method, options = {}) ⇒ Object
Bootstrap wrapper for readonly text field that is shown as plain text.
plaintext(:value)
265 266 267 268 269 270 271 272 273 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 265 def plaintext(method, = {}) bootstrap = form_bootstrap.scoped(.delete(:bootstrap)) draw_form_group(bootstrap, method, ) do remove_css_class!(, "form-control") add_css_class!(, "form-control-plaintext") [:readonly] = true ActionView::Helpers::FormBuilder.instance_method(:text_field).bind(self).call(method, ) end end |
#primary(value = nil, options = {}, &block) ⇒ Object
Same as submit button, only with btn-primary class added
311 312 313 314 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 311 def primary(value = nil, = {}, &block) add_css_class!(, "btn-primary") submit(value, , &block) end |
#radio_button(method, tag_value, options = {}) ⇒ Object
Wrapper around radio button. Example usage:
:choice, "value", bootstrap: {label: {text: "Do you agree?"}}
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 130 def (method, tag_value, = {}) bootstrap = form_bootstrap.scoped(.delete(:bootstrap)) return super if bootstrap.disabled help_text = draw_help(bootstrap) errors = draw_errors(bootstrap, method) add_css_class!(, "form-check-input") add_css_class!(, "is-invalid") if errors.present? label_text = nil if (custom_text = bootstrap.label[:text]).present? label_text = custom_text end fieldset_css_class = "form-group" fieldset_css_class += " row" if bootstrap.horizontal? fieldset_css_class += " #{bootstrap.inline_margin_class}" if bootstrap.inline? content_tag(:fieldset, class: fieldset_css_class) do draw_control_column(bootstrap, offset: true) do if bootstrap.custom_control content_tag(:div, class: "custom-control custom-radio") do add_css_class!(, "custom-control-input") remove_css_class!(, "form-check-input") concat super(method, tag_value, ) concat label(method, label_text, value: tag_value, class: "custom-control-label") concat errors if errors.present? concat help_text if help_text.present? end else content_tag(:div, class: "form-check") do concat super(method, tag_value, ) concat label(method, label_text, value: tag_value, class: "form-check-label") concat errors if errors.present? concat help_text if help_text.present? end end end end end |
#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object
Wrapper for select helper. Boostrap options are sent via options hash:
select :choices, ["a", "b"], bootstrap: {label: {text: "Custom"}}
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 89 def select(method, choices = nil, = {}, = {}, &block) bootstrap = form_bootstrap.scoped(.delete(:bootstrap)) return super if bootstrap.disabled add_css_class!(, "custom-select") if bootstrap.custom_control draw_form_group(bootstrap, method, ) do super(method, choices, , , &block) end end |
#submit(value = nil, options = {}, &block) ⇒ Object
Add bootstrap formatted submit button. If you need to change its type or add another css class, you need to override all css classes like so:
submit(class: "btn btn-info custom-class")
You may add additional content that directly follows the button. Here’s an example of a cancel link:
submit do
link_to("Cancel", "/", class: "btn btn-link")
end
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/comfy_bootstrap_form/form_builder.rb', line 287 def submit(value = nil, = {}, &block) if value.is_a?(Hash) = value value = nil end bootstrap = form_bootstrap.scoped(.delete(:bootstrap)) return super if bootstrap.disabled add_css_class!(, "btn") form_group_class = "form-group" form_group_class += " row" if bootstrap.horizontal? content_tag(:div, class: form_group_class) do draw_control_column(bootstrap, offset: true) do out = super(value, ) out << capture(&block) if block_given? out end end end |