Class: Lotus::Helpers::FormHelper::FormBuilder
- Inherits:
-
HtmlHelper::HtmlBuilder
- Object
- HtmlHelper::HtmlBuilder
- Lotus::Helpers::FormHelper::FormBuilder
- Defined in:
- lib/lotus/helpers/form_helper/form_builder.rb
Overview
Form builder
Constant Summary collapse
- BROWSER_METHODS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Set of HTTP methods that are understood by web browsers
['GET', 'POST'].freeze
- CHECKED =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Checked attribute value
'checked'.freeze
- SELECTED =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Selected attribute value for option
'selected'.freeze
- ACCEPT_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Separator for accept attribute of file input
','.freeze
- INPUT_ID_REPLACEMENT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Replacement for input id interpolation
'-\k<token>'.freeze
- INPUT_VALUE_REPLACEMENT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Replacement for input value interpolation
'.\k<token>'.freeze
- DEFAULT_UNCHECKED_VALUE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default value for unchecked check box
'0'.freeze
- DEFAULT_CHECKED_VALUE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default value for checked check box
'1'.freeze
Constants inherited from HtmlHelper::HtmlBuilder
HtmlHelper::HtmlBuilder::CONTENT_TAGS, HtmlHelper::HtmlBuilder::EMPTY_TAGS, HtmlHelper::HtmlBuilder::NEWLINE
Instance Method Summary collapse
-
#check_box(name, attributes = {}) ⇒ Object
Check box.
-
#color_field(name, attributes = {}) ⇒ Object
Color input.
-
#date_field(name, attributes = {}) ⇒ Object
Date input.
-
#datetime_field(name, attributes = {}) ⇒ Object
Datetime input.
-
#datetime_local_field(name, attributes = {}) ⇒ Object
Datetime Local input.
-
#email_field(name, attributes = {}) ⇒ Object
Email input.
-
#fields_for(name) ⇒ Object
Nested fields.
-
#file_field(name, attributes = {}) ⇒ Object
File input.
-
#hidden_field(name, attributes = {}) ⇒ Object
Hidden input.
-
#initialize(form, attributes, context = nil, &blk) ⇒ Lotus::Helpers::FormHelper::FormBuilder
constructor
private
Instantiate a form builder.
-
#label(content, attributes = {}) ⇒ Object
Label tag.
-
#password_field(name, attributes = {}) ⇒ Object
Password input.
-
#radio_button(name, value, attributes = {}) ⇒ Object
Radio input.
-
#select(name, values, attributes = {}) ⇒ Object
Select input.
-
#submit(content, attributes = {}) ⇒ Object
Submit button.
-
#text_field(name, attributes = {}) ⇒ Object
(also: #input_text)
Text input.
-
#to_s ⇒ Lotus::Utils::Escape::SafeString
private
Resolves all the nodes and generates the markup.
Methods inherited from HtmlHelper::HtmlBuilder
#empty_tag, #method_missing, #nested?, #resolve, #tag
Constructor Details
#initialize(form, attributes, params, &blk) ⇒ Lotus::Helpers::FormHelper::FormBuilder #initialize(form, attributes, params, &blk) ⇒ Lotus::Helpers::FormHelper::FormBuilder
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Instantiate a form builder
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 101 def initialize(form, attributes, context = nil, &blk) super() @context = context @blk = blk # Nested form if @context.nil? && attributes.is_a?(Values) @values = attributes @attributes = {} @name = form else @form = form @name = form.name @values = Values.new(form.values, @context.params) @attributes = attributes @csrf_token = csrf_token end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Lotus::Helpers::HtmlHelper::HtmlBuilder
Instance Method Details
#check_box(name, attributes = {}) ⇒ Object
Check box
It renders a check box input.
When a form is submitted, browsers don’t send the value of unchecked check boxes. If an user unchecks a check box, their browser won’t send the unchecked value. On the server side the corresponding value is missing, so the application will assume that the user action never happened.
To solve this problem the form renders a hidden field with the “unchecked value”. When the user unchecks the input, the browser will ignore it, but it will still send the value of the hidden input. See the examples below.
When editing a resource, the form automatically assigns the checked="checked"
attribute.
342 343 344 345 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 342 def check_box(name, attributes = {}) _hidden_field_for_check_box( name, attributes) input _attributes_for_check_box(name, attributes) end |
#color_field(name, attributes = {}) ⇒ Object
Color input
362 363 364 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 362 def color_field(name, attributes = {}) input _attributes(:color, name, attributes) end |
#date_field(name, attributes = {}) ⇒ Object
Date input
381 382 383 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 381 def date_field(name, attributes = {}) input _attributes(:date, name, attributes) end |
#datetime_field(name, attributes = {}) ⇒ Object
Datetime input
400 401 402 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 400 def datetime_field(name, attributes = {}) input _attributes(:datetime, name, attributes) end |
#datetime_local_field(name, attributes = {}) ⇒ Object
Datetime Local input
419 420 421 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 419 def datetime_local_field(name, attributes = {}) input _attributes(:'datetime-local', name, attributes) end |
#email_field(name, attributes = {}) ⇒ Object
Email input
438 439 440 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 438 def email_field(name, attributes = {}) input _attributes(:email, name, attributes) end |
#fields_for(name) ⇒ Object
Nested fields
The inputs generated by the wrapped block will be prefixed with the given name It supports infinite levels of nesting.
197 198 199 200 201 202 203 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 197 def fields_for(name) current_name = @name @name = _input_name(name) yield ensure @name = current_name end |
#file_field(name, attributes = {}) ⇒ Object
File input
PLEASE REMEMBER TO ADD enctype: 'multipart/form-data'
ATTRIBUTE TO THE FORM
497 498 499 500 501 502 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 497 def file_field(name, attributes = {}) attributes[:accept] = Array(attributes[:accept]).join(ACCEPT_SEPARATOR) if attributes.key?(:accept) attributes = { type: :file, name: _input_name(name), id: _input_id(name) }.merge(attributes) input(attributes) end |
#hidden_field(name, attributes = {}) ⇒ Object
Hidden input
457 458 459 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 457 def hidden_field(name, attributes = {}) input _attributes(:hidden, name, attributes) end |
#label(content, attributes = {}) ⇒ Object
Label tag
The first param content
can be a Symbol
that represents the target field (Eg. :extended_title
), or a String
which is used as it is.
# Output:
# <label for="book-extended-title">Extended Title</label>
# Output:
# <label for="book-extended-title">Title</label>
# Output:
# <label for="delivery-address-city">City</label>
# <input type="text" name="delivery[address][city] id="delivery-address-city" value="">
246 247 248 249 250 251 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 246 def label(content, attributes = {}) attributes = { for: _for(content, attributes.delete(:for)) }.merge(attributes) content = Utils::String.new(content).titleize super(content, attributes) end |
#password_field(name, attributes = {}) ⇒ Object
Password input
584 585 586 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 584 def password_field(name, attributes = {}) input({ type: :password, name: _input_name(name), id: _input_id(name), value: nil }.merge(attributes)) end |
#radio_button(name, value, attributes = {}) ⇒ Object
Radio input
If request params have a value that corresponds to the given value, it automatically sets the checked
attribute. This Lotus::Controller integration happens without any developer intervention.
563 564 565 566 567 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 563 def (name, value, attributes = {}) attributes = { type: :radio, name: _input_name(name), value: value }.merge(attributes) attributes[:checked] = CHECKED if _value(name) == value input(attributes) end |
#select(name, values, attributes = {}) ⇒ Object
Select input
If request params have a value that corresponds to one of the given values, it automatically sets the selected
attribute on the <option>
tag. This Lotus::Controller integration happens without any developer intervention.
632 633 634 635 636 637 638 639 640 641 642 643 644 645 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 632 def select(name, values, attributes = {}) = attributes.delete(:options) || {} attributes = { name: _input_name(name), id: _input_id(name) }.merge(attributes) super(attributes) do values.each do |value, content| if _value(name) == value option(content, {value: value, selected: SELECTED}.merge()) else option(content, {value: value}.merge()) end end end end |
#submit(content, attributes = {}) ⇒ Object
Submit button
662 663 664 665 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 662 def submit(content, attributes = {}) attributes = { type: :submit }.merge(attributes) (content, attributes) end |
#text_field(name, attributes = {}) ⇒ Object Also known as: input_text
Text input
519 520 521 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 519 def text_field(name, attributes = {}) input _attributes(:text, name, attributes) end |
#to_s ⇒ Lotus::Utils::Escape::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolves all the nodes and generates the markup
130 131 132 133 134 135 136 137 |
# File 'lib/lotus/helpers/form_helper/form_builder.rb', line 130 def to_s if toplevel? _method_override! form(@blk, @attributes) end super end |