Class: ComfyBootstrapForm::BootstrapOptions
- Inherits:
-
Object
- Object
- ComfyBootstrapForm::BootstrapOptions
- Defined in:
- lib/comfy_bootstrap_form/bootstrap_options.rb
Overview
Container for bootstrap specific form builder options. It controls options that define form layout and grid sizing. They are passed-in into form helper and field helpers via ‘:bootstrap` option. For example:
bootstrap_form_with scope: :login, url: "/login", bootstrap: {layout: :inline} do |f|
f.text_field :username, bootstrap: {label: {text: "Your username"}}
end
Instance Attribute Summary collapse
-
#append ⇒ Object
Returns the value of attribute append.
-
#check_inline ⇒ Object
Options to render checkboxes and radio buttons inline.
-
#control_col_class ⇒ Object
CSS class for control column when using horizontal form.
-
#custom_control ⇒ Object
Enables special input styling for file_field, radio and checkboxes.
-
#disabled ⇒ Object
When set to ‘true` only default rails form builder element is rendered.
-
#error ⇒ Object
Manually rendering the error message.
-
#help ⇒ Object
Help text that goes under the form field.
-
#inline_margin_class ⇒ Object
CSS class used to space out form groups for inline forms.
-
#label ⇒ Object
Label specific options.
-
#label_align_class ⇒ Object
CSS class for label alignment in horizontal form.
-
#label_col_class ⇒ Object
CSS class for label column when using horizontal form.
-
#layout ⇒ Object
Controls form layout.
-
#prepend ⇒ Object
Input groups allow prepending and appending arbitrary html.
Instance Method Summary collapse
- #horizontal? ⇒ Boolean
-
#initialize(options = {}) ⇒ BootstrapOptions
constructor
A new instance of BootstrapOptions.
- #inline? ⇒ Boolean
- #offset_col_class ⇒ Object
-
#scoped(options = {}) ⇒ Object
This will return a copy of BootstrapOptions object with new options set that don’t affect original object.
- #set_options(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ BootstrapOptions
Returns a new instance of BootstrapOptions.
78 79 80 81 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 78 def initialize( = {}) set_defaults () end |
Instance Attribute Details
#append ⇒ Object
Returns the value of attribute append.
52 53 54 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 52 def append @append end |
#check_inline ⇒ Object
Options to render checkboxes and radio buttons inline. Default is false. Example:
form. :choices, ["yes", "no"], :to_s, :to_s, bootstrap: {check_inline: true}
64 65 66 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 64 def check_inline @check_inline end |
#control_col_class ⇒ Object
CSS class for control column when using horizontal form. Default: “col-sm-10”
24 25 26 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 24 def control_col_class @control_col_class end |
#custom_control ⇒ Object
Enables special input styling for file_field, radio and checkboxes. Example:
form.file_file :photo, bootstrap: {custom_control: true}
70 71 72 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 70 def custom_control @custom_control end |
#disabled ⇒ Object
When set to ‘true` only default rails form builder element is rendered.
15 16 17 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 15 def disabled @disabled end |
#error ⇒ Object
Manually rendering the error message. Example:
form.text_field :foo, bootstrap: {error: "Error Message"}
76 77 78 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 76 def error @error end |
#help ⇒ Object
Help text that goes under the form field. Example usage:
form.password_field :password, bootstrap: {help: "Password should be more than 8 characters in length"}
58 59 60 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 58 def help @help end |
#inline_margin_class ⇒ Object
CSS class used to space out form groups for inline forms. Default: “mr-sm-2”
30 31 32 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 30 def inline_margin_class @inline_margin_class end |
#label ⇒ Object
Label specific options. Default is and empty hash. Options are as follows:
text: "Label Text" - override automatically generated label text
hide: true - label only visible to screen readers
class: "custom" - append custom CSS class
Example:
form.label :username, bootstrap: {label: {text: "Name", class: "important"}}
40 41 42 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 40 def label @label end |
#label_align_class ⇒ Object
CSS class for label alignment in horizontal form. Default: “text-sm-right”
27 28 29 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 27 def label_align_class @label_align_class end |
#label_col_class ⇒ Object
CSS class for label column when using horizontal form. Default: “col-sm-2”
21 22 23 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 21 def label_col_class @label_col_class end |
#layout ⇒ Object
Controls form layout. Can be: “vertical” (default), “horizontal” or “inline”
18 19 20 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 18 def layout @layout end |
#prepend ⇒ Object
Input groups allow prepending and appending arbitrary html. By default these are nil. Example usage:
form.text_field :dollars, bootstrap: {prepend: "$", append: ".00"}
For non-text values, use hash like so:
form.text_field :search, bootstrap: {append: {html: "<button>Go</button>".html_safe}}
51 52 53 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 51 def prepend @prepend end |
Instance Method Details
#horizontal? ⇒ Boolean
83 84 85 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 83 def horizontal? @layout.to_s == "horizontal" end |
#inline? ⇒ Boolean
87 88 89 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 87 def inline? @layout.to_s == "inline" end |
#offset_col_class ⇒ Object
91 92 93 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 91 def offset_col_class label_col_class.gsub(%r{col-(\w+)-(\d+)}, 'offset-\1-\2') end |
#scoped(options = {}) ⇒ Object
This will return a copy of BootstrapOptions object with new options set that don’t affect original object. This way we can have options specific to a given form field. For example, we can change grid just for one field:
bootstrap_form_with scope: :login do |f|
f.text_field :email, bootstrap: {label_col_class: "col-md-6", control_col_class: "col-md-6"}
f.password_field :password
end
104 105 106 107 108 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 104 def scoped( = {}) scope = clone scope.() scope end |
#set_options(options = {}) ⇒ Object
110 111 112 113 114 |
# File 'lib/comfy_bootstrap_form/bootstrap_options.rb', line 110 def ( = {}) .is_a?(Hash) && .each do |key, value| public_send("#{key}=", value) end end |