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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 80 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
96 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 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 96 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 label = [:label] == false ? ''.html_safe : field_label(method, ) 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 126 def date_select method, = {}, = {} field = super return field if [:vanilla] errors, error_msg = render_errors method #hmm, apparently TODO help_text label = [:label] == false ? ''.html_safe : field_label(method, .except(:class)) 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
192 193 194 195 196 197 198 199 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 192 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, @options.merge(), wrapper, &block) end |
#grouped_inputs(field_options = {}, &block) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 180 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, @options, wrapper, &block) end |
#inline_inputs(field_options = {}, &block) ⇒ Object
input groups
170 171 172 173 174 175 176 177 178 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 170 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, @options.merge(), wrapper, &block) end |
#radio_button(method, value, options = {}) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 147 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
159 160 161 162 163 164 165 166 |
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 159 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 |