Class: BootstrapFormBuilder::HorizontalFormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- BootstrapFormBuilder::HorizontalFormBuilder
- Defined in:
- lib/bootstrap_form_builder/horizontal_form_builder.rb
Overview
A form build that adheres to Bootstrap horizontal form conventions.
Instance Method Summary collapse
- #cancel_button(cancel_path) ⇒ Object
- #check_box(name, opts = {}) ⇒ Object
- #col_wrap(html) ⇒ Object
- #date_field(name, opts = {}) ⇒ Object
- #email_field(name, opts = {}) ⇒ Object
- #errors(name) ⇒ Object
- #form_group(name, options = {}, &block) ⇒ Object
- #help(name) ⇒ Object
-
#justified_radio_button_group(name, button_options, opts = {}) ⇒ Object
uses bootstrap option to stretch the buttons to the full enclosing width if you use this, you may need to add the following style to your stylesheet to re-hide the radio-button circle (because Bootstrap’s one is too specific to deal with this):.
- #label(name, opts = {}) ⇒ Object
- #label_description(name) ⇒ Object
- #number_field(name, opts = {}) ⇒ Object
- #password_field(name, opts = {}) ⇒ Object
- #radio_button_group(name, button_options, opts = {}) ⇒ Object
- #radio_button_label(name, button) ⇒ Object
- #radio_group(name, button_options, opts = {}) ⇒ Object
- #radio_label(name, button) ⇒ Object
- #search_field(name, opts = {}) ⇒ Object
- #select(name, choices, options = {}, html_options = {}) ⇒ Object
-
#simple_check_box(name, options = {}, checked_value = '1', unchecked_value = '0') ⇒ Object
A checkbox without the form group and control label.
- #submit_and_cancel(cancel_path) ⇒ Object
- #submit_button(label = "", opts = {}) ⇒ Object
- #text_area(name, opts = {}) ⇒ Object
- #text_field(name, opts = {}) ⇒ Object
- #tip(name, options = {}) ⇒ Object
- #validation_attributes(name) ⇒ Object
Instance Method Details
#cancel_button(cancel_path) ⇒ Object
225 226 227 228 229 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 225 def (cancel_path) @template.link_to(I18n.t('helpers.button.cancel'), cancel_path, :class => 'btn btn-default') end |
#check_box(name, opts = {}) ⇒ Object
61 62 63 64 65 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 61 def check_box(name, opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do simple_check_box(name, opts) end end |
#col_wrap(html) ⇒ Object
182 183 184 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 182 def col_wrap(html) @template.content_tag(:div, html, :class => 'field') end |
#date_field(name, opts = {}) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 45 def date_field(name, opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do super(name, opts.reverse_merge(:class => 'form-control', :placeholder => help(name)). reverse_merge(validation_attributes(name))) end end |
#email_field(name, opts = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 4 def email_field(name, opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do super(name, opts.reverse_merge(:class => 'form-control', :autocorrect => 'off', :autocapitalize => 'off', :autocorrect => 'off', :spellcheck => 'off', :placeholder => help(name)). reverse_merge(validation_attributes(name))) end end |
#errors(name) ⇒ Object
192 193 194 195 196 197 198 199 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 192 def errors(name) if @object.errors.has_key?(name) errors = @object.errors.(name).join('. ') @template.content_tag(:span, errors, :class => 'help-text text-danger') else '' end end |
#form_group(name, options = {}, &block) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 167 def form_group(name, = {}, &block) = .fetch(:group_options, {}) classes = Array([:class]) << 'form-group' if @object.errors.has_key?(name) classes << 'has-error' end @template.content_tag(:div, label(name, .fetch(:label_options, {})) + col_wrap(block.call + errors(name)) + tip(name, .fetch(:tip_options, {})), .merge(:class => classes.join(' '))) end |
#help(name) ⇒ Object
201 202 203 204 205 206 207 208 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 201 def help(name) i18n_name = "helpers.hints.#{object_name}.#{name}" if I18n.exists?(i18n_name) I18n.t(i18n_name) else nil end end |
#justified_radio_button_group(name, button_options, opts = {}) ⇒ Object
uses bootstrap option to stretch the buttons to the full enclosing width if you use this, you may need to add the following style to your stylesheet to re-hide the radio-button circle (because Bootstrap’s one is too specific to deal with this):
- data-toggle=“buttons”
-
.btn input
position: absolute;
z-index: -1;
opacity: 0;
filter: alpha(opacity=0);
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 91 def (name, , opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do = .map do || @template.content_tag(:div, (name, ), :class => 'btn-group') end.join("\n").html_safe @template.content_tag(:div, , :class => 'btn-group btn-group-justified', :data => { :toggle => 'buttons' }) end end |
#label(name, opts = {}) ⇒ Object
186 187 188 189 190 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 186 def label(name, opts = {}) classes = Array(opts.fetch(:class, nil)) classes << 'control-label' if classes.empty? super(name, opts.merge(:class => classes.join(' '))) end |
#label_description(name) ⇒ Object
210 211 212 213 214 215 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 210 def label_description(name) # use description if provided, default to the label-text desc = I18n.t("helpers.label.#{object_name}.#{name}_description", :default => "") return desc if desc.present? I18n.t("helpers.label.#{object_name}.#{name}", :default => "") end |
#number_field(name, opts = {}) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 53 def number_field(name, opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do super(name, opts.reverse_merge(:class => 'form-control', :placeholder => help(name)). reverse_merge(validation_attributes(name))) end end |
#password_field(name, opts = {}) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 37 def password_field(name, opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do super(name, opts.reverse_merge(:class => 'form-control', :placeholder => help(name)). reverse_merge(validation_attributes(name))) end end |
#radio_button_group(name, button_options, opts = {}) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 104 def (name, , opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do = .map do || (name, ) end.join("\n").html_safe @template.content_tag(:div, , :class => 'btn-group', :data => { :toggle => 'buttons' }. merge(validation_attributes(name))) end end |
#radio_button_label(name, button) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 118 def (name, ) label(name, :value => , :class => 'btn btn-default') do (name, ) + I18n.t("#{object_name}.#{name}_options.#{}", :scope => "helpers.label") end end |
#radio_group(name, button_options, opts = {}) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 126 def radio_group(name, , opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do = .map do || radio_label(name, ) end.join("\n").html_safe @template.content_tag(:div, , :class => 'radio-group', :data => validation_attributes(name)) end end |
#radio_label(name, button) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 139 def radio_label(name, ) label = label("#{name}_#{}", :class => 'radio-label') do (name, ) + I18n.t("#{object_name}.#{name}_options.#{}", :scope => "helpers.label") end @template.content_tag(:div, label, :class => 'radio') end |
#search_field(name, opts = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 24 def search_field(name, opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do @template.content_tag(:div, super(name, opts.reverse_merge(:class => 'form-control', :placeholder => help(name)). reverse_merge(validation_attributes(name))) + @template.content_tag(:span, @template.content_tag(:span, '', :class => 'glyphicon glyphicon-search'), :class => 'input-group-addon'), :class => 'input-group') end end |
#select(name, choices, options = {}, html_options = {}) ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 151 def select(name, choices, = {}, = {}) form_group(name, .slice(:label_options, :group_options, :tip_options)) do super(name, choices, , .reverse_merge(:class => 'form-control'). reverse_merge(validation_attributes(name))) end end |
#simple_check_box(name, options = {}, checked_value = '1', unchecked_value = '0') ⇒ Object
A checkbox without the form group and control label
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 68 def simple_check_box(name, = {}, checked_value = '1', unchecked_value = '0') @template.content_tag(:div, @template.content_tag(:label, @template.check_box(@object_name, name, (), checked_value, unchecked_value) + label_description(name).html_safe), :class => 'checkbox', :data => validation_attributes(name)) end |
#submit_and_cancel(cancel_path) ⇒ Object
240 241 242 243 244 245 246 247 248 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 240 def submit_and_cancel(cancel_path) @template.content_tag(:div, @template. content_tag(:div, submit(:class => 'btn btn-primary') + " " + (cancel_path), :class => 'button-group'), :class => 'form-group') end |
#submit_button(label = "", opts = {}) ⇒ Object
231 232 233 234 235 236 237 238 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 231 def (label = "", opts = {}) @template.content_tag(:div, @template. content_tag(:div, submit(label, opts.merge(:class => 'btn btn-primary')), :class => 'button-group'), :class => 'form-group') end |
#text_area(name, opts = {}) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 159 def text_area(name, opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do super(name, opts.reverse_merge(:class => 'form-control', :placeholder => help(name)). reverse_merge(validation_attributes(name))) end end |
#text_field(name, opts = {}) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 16 def text_field(name, opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do super(name, opts.reverse_merge(:class => 'form-control', :placeholder => help(name)). reverse_merge(validation_attributes(name))) end end |
#tip(name, options = {}) ⇒ Object
217 218 219 220 221 222 223 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 217 def tip(name, = {}) i18n_name = "helpers.tips.#{object_name}.#{name}" if I18n.exists?(i18n_name) [:class] = (Array([:class]) + ['help-block']).join(' ') @template.content_tag(:span, I18n.t(i18n_name), ) end end |
#validation_attributes(name) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 250 def validation_attributes(name) return {} unless [:validations] return {} unless object.respond_to?(:_validators) validation_attribute_map = { ActiveModel::Validations::PresenceValidator => proc { { :required => true } }, ActiveModel::Validations::InclusionValidator => proc {|validator| # Inclusion is weird, in Rails if you have a radio button # where you want one of the options selected, and they map # to a boolean then using :presence doesn't work, because # the presence check on false fails. Instead you have to use # inclusion, so we can handle this here and map it to required # validation attribute, this is kinda getting a bit hacky though, # and may come back to bite us in the foot, so we limit the use # to only those cases where true and false are the only options. if validator.[:in] == [true,false] { :required => true } else { } end }, ActiveModel::Validations::FormatValidator => proc {|validator| # Because RegExp#to_s is weird we use inspect, but strip first and last / { :'data-pattern' => validator.[:with].inspect[1..-2], :'data-pattern-message' => validator.[:message] } } } validators = object._validators.fetch(name, []) validators.reduce({}) do |attributes, validator| attributes.merge(validation_attribute_map.fetch(validator.class, proc {}).call(validator)) end end |