Class: BootstrapsBootstraps::BootstrapFormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- BootstrapsBootstraps::BootstrapFormBuilder
- Defined in:
- lib/bootstraps_bootstraps/bootstrap_form_builder.rb
Overview
This class started out as a gist somewhere but I can’t seem to find the
original. At any rate its been modified over time to make it not break
and to update it to make it compatible with Bootstrap2.
If I'm being brutally honest its a bit of a cluster of metaprogramming
and it can be pretty difficult to understand. BUT it does seem to work
so far.
Instance Method Summary collapse
- #check_box(method, options = {}, checked_value = 1, unchecked_value = 0) ⇒ Object
- #collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
- #date_select(method, options = {}, html_options = {}) ⇒ Object
- #form_actions(field_options = {}, &block) ⇒ Object
- #grouped_inputs(field_options = {}, &block) ⇒ Object
-
#initialize(object_name, object, template, options, block) ⇒ BootstrapFormBuilder
constructor
A new instance of BootstrapFormBuilder.
-
#inline_inputs(field_options = {}, &block) ⇒ Object
input groups.
- #radio_button(method, value, options = {}) ⇒ Object
- #submit(method, options = {}) ⇒ Object
Constructor Details
#initialize(object_name, object, template, options, block) ⇒ BootstrapFormBuilder
Returns a new instance of BootstrapFormBuilder.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 30 def initialize(object_name, object, template, , block) super(object_name, object, template, , block) @form_mode = :vertical #default value @form_mode = :horizontal if [:horizontal] || detect_html_class(, 'form-horizontal') @form_mode = :search if [:search] || detect_html_class(, 'form-search') @form_mode = :inline if [:inline] || detect_html_class(, 'form-inline') @action_wrapped = [:action_wrapped] end |
Instance Method Details
#check_box(method, options = {}, checked_value = 1, unchecked_value = 0) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 81 def check_box method, = {}, checked_value = 1, unchecked_value = 0 if [:vanilla] return super end guarantee_html_class , :checkbox [:class].push 'inline' if @form_mode == :inline text = [:label] || '' .delete :label field_label(method, ) do super(method,,checked_value,unchecked_value) + text end end |
#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 97 def collection_select method, collection, value_method, text_method, = {}, = {} #abort and just let the rails formbuilder do its thing if [:vanilla] return super end errors, error_msg = render_errors method = .clone guarantee_html_class , 'control-label' name = [:label] || method label = [:label] == false ? ''.html_safe : field_label([:label], ) guarantee_html_class [:class].push 'input-xlarge' if [:large] [:class].push 'inline' if @form_mode == :inline .delete :label field = super(method,collection,value_method,text_method,,) + ' ' + error_msg #wrap it in div.controls field = div_with_class(['controls',errors], :content => field) if @form_mode == :horizontal #tack on the label field = label + field unless @form_mode == :inline #wrap it in div.control-group field = div_with_class(['control-group',errors], :content => field) if @form_mode == :horizontal field end |
#date_select(method, options = {}, html_options = {}) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 130 def date_select method, = {}, = {} field = super return field if [:vanilla] errors, error_msg = render_errors method #hmm, apparently TODO help_text = .clone [:class] = ['control-label'] #guarantee_html_class label_options, 'control-label' label = [:label] == false ? ''.html_safe : field_label(method, ) guarantee_html_class , 'inline' field += ' '.html_safe + error_msg field = div_with_class(['controls',errors], :content => field) if @form_mode == :horizontal field = label + field field = div_with_class(['control-group',errors], :content => field) if @form_mode == :horizontal field end |
#form_actions(field_options = {}, &block) ⇒ Object
199 200 201 202 203 204 205 206 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 199 def form_actions = {}, &block [:action_wrapped] = true wrapper = lambda { |content| field_group = div_with_class('form-actions', content: content) } @template.wrapped_inputs(@object_name, @object, .merge(), wrapper, &block) end |
#grouped_inputs(field_options = {}, &block) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 187 def grouped_inputs = {}, &block wrapper = lambda do |content| field_group = div_with_class('controls', content:content) if [:label] field_group = label( [:label], [:label], class: 'control-label') + field_group end field_group = div_with_class('control-group', content:field_group) end @template.wrapped_inputs(@object_name, @object, , wrapper, &block) end |
#inline_inputs(field_options = {}, &block) ⇒ Object
input groups
177 178 179 180 181 182 183 184 185 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 177 def inline_inputs = {}, &block [:inline] = true wrapper = lambda { |content| field_group = div_with_class('controls', content: content) field_group = label( [:label], [:label], class: 'control-label' ) + field_group field_group = div_with_class('control-group', content: field_group) } @template.wrapped_inputs(@object_name, @object, .merge(), wrapper, &block) end |
#radio_button(method, value, options = {}) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 154 def method, value, = {} guarantee_html_class , 'radio' error_class, = render_errors method text = [:label] || '' .delete :label content_tag(:label, class: [:class]) do super(method,value,) + text end end |
#submit(method, options = {}) ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 166 def submit method, = {} # options block gets stringify_keys called on it in the first super, no :keys exist after that field = super return field if ['vanilla'] field = div_with_class('form-actions', content: field) unless ['no_action_block'] || @action_wrapped || [:inline, :search].include?(@form_mode) puts @action_wrapped field end |