Class: BootstrapFormBuilder::HorizontalFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
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

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_button(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.(:div,
                          @template.(: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.(: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.full_messages_for(name).join('. ')
    @template.(: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, options = {}, &block)
  group_options = options.fetch(:group_options, {})
  classes = Array(group_options[:class]) << 'form-group'

  if @object.errors.has_key?(name)
    classes << 'has-error'
  end

  @template.(:div,
                        label(name, options.fetch(:label_options, {})) +
                        col_wrap(block.call + errors(name)) +
                        tip(name, options.fetch(:tip_options, {})),
                        group_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 justified_radio_button_group(name, button_options, opts = {})
  form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do
    buttons = button_options.map do |button|
      @template.(:div, radio_button_label(name, button), :class => 'btn-group')
    end.join("\n").html_safe

    @template.(:div,
                          buttons,
                          :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 radio_button_group(name, button_options, opts = {})
  form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do
    buttons = button_options.map do |button|
      radio_button_label(name, button)
    end.join("\n").html_safe

    @template.(:div,
                          buttons,
                          :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 radio_button_label(name, button)
  label(name, :value => button, :class => 'btn btn-default') do
    radio_button(name, button) +
      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.(:div,
                super(name, opts.reverse_merge(:class => 'form-control',
                                               :placeholder => help(name))) +
                @template.(:span,
                                      @template.(: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, options = {}, html_options = {})
  form_group(name, options.slice(:label_options, :group_options, :tip_options)) do
    super(name, choices, options, html_options.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.(:div,
                        @template.
                          (:div,
                                      submit(:class => 'btn btn-primary') +
                                      " " + cancel_button(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 submit_button(label = "")
  @template.(:div,
                        @template.
                          (: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, options = {})
  i18n_name = "helpers.tips.#{object_name}.#{name}"
  if I18n.exists?(i18n_name)
    options[:class] = (Array(options[:class]) + ['help-block']).join(' ')
    @template.(:span, I18n.t(i18n_name), options)
  end
end