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
- #number_field(name, opts = {}) ⇒ Object
- #password_field(name, opts = {}) ⇒ Object
- #radio_button_group(name, button_options, opts = {}) ⇒ Object
- #radio_button_label(name, button) ⇒ Object
- #search_field(name, opts = {}) ⇒ Object
- #select(name, choices, options = {}, html_options = {}) ⇒ Object
- #submit_and_cancel(cancel_path) ⇒ Object
- #submit_button(label = "") ⇒ Object
- #text_area(name, opts = {}) ⇒ Object
- #text_field(name, opts = {}) ⇒ Object
- #tip(name, options = {}) ⇒ Object
Instance Method Details
#cancel_button(cancel_path) ⇒ Object
169 170 171 172 173 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 169 def (cancel_path) @template.link_to(I18n.t('helpers.button.cancel'), cancel_path, :class => 'btn btn-default') end |
#check_box(name, opts = {}) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 51 def check_box(name, opts = {}) form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do @template.content_tag(:div, @template.content_tag(:label, super(name, opts) + (help(name) || " ").html_safe), :class => 'checkbox') end end |
#col_wrap(html) ⇒ Object
133 134 135 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 133 def col_wrap(html) @template.content_tag(:div, html, :class => 'field') end |
#date_field(name, opts = {}) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 37 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))) end end |
#email_field(name, opts = {}) ⇒ Object
4 5 6 7 8 9 |
# 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', :placeholder => help(name))) end end |
#errors(name) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 143 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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 118 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
152 153 154 155 156 157 158 159 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 152 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);
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 71 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
137 138 139 140 141 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 137 def label(name, opts = {}) classes = Array(opts.fetch(:class, '')) classes << 'control-label' super(name, opts.merge(:class => classes.join(' '))) end |
#number_field(name, opts = {}) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 44 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))) end end |
#password_field(name, opts = {}) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 30 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))) end end |
#radio_button_group(name, button_options, opts = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 84 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' }) end end |
#radio_button_label(name, button) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 97 def (name, ) label(name, :value => , :class => 'btn btn-default') do (name, ) + I18n.t("#{object_name}.#{name}_options.#{button}", :scope => "helpers.label") end end |
#search_field(name, opts = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 18 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))) + @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
105 106 107 108 109 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 105 def select(name, choices, = {}, = {}) form_group(name, .slice(:label_options, :group_options, :tip_options)) do super(name, choices, , .reverse_merge(:class => 'form-control')) end end |
#submit_and_cancel(cancel_path) ⇒ Object
184 185 186 187 188 189 190 191 192 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 184 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 = "") ⇒ Object
175 176 177 178 179 180 181 182 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 175 def (label = "") @template.content_tag(:div, @template. content_tag(:div, submit(label, :class => 'btn btn-primary'), :class => 'button-group'), :class => 'form-group') end |
#text_area(name, opts = {}) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 111 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))) end end |
#text_field(name, opts = {}) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 11 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))) end end |
#tip(name, options = {}) ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/bootstrap_form_builder/horizontal_form_builder.rb', line 161 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 |