Class: BootstrapForm::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Extended by:
Aliasing
Includes:
Helpers::Bootstrap
Defined in:
lib/bootstrap_form/form_builder.rb

Constant Summary collapse

FIELD_HELPERS =
%w{color_field date_field datetime_field datetime_local_field
email_field month_field number_field password_field phone_field
range_field search_field telephone_field text_area text_field time_field
url_field week_field}
DATE_SELECT_HELPERS =
%w{date_select time_select datetime_select}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Aliasing

bootstrap_method_alias

Methods included from Helpers::Bootstrap

#alert_message, #button, #custom_control, #error_summary, #errors_on, #input_group_content, #input_with_error, #prepend_and_append_input, #primary, #static_class, #static_control, #submit

Constructor Details

#initialize(object_name, object, template, options) ⇒ FormBuilder

Returns a new instance of FormBuilder


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bootstrap_form/form_builder.rb', line 20

def initialize(object_name, object, template, options)
  @layout = options[:layout]
  @label_col = options[:label_col] || default_label_col
  @control_col = options[:control_col] || default_control_col
  @label_errors = options[:label_errors] || false
  @inline_errors = if options[:inline_errors].nil?
    @label_errors != true
  else
    options[:inline_errors] != false
  end
  @acts_like_form_tag = options[:acts_like_form_tag]

  super
end

Instance Attribute Details

#acts_like_form_tagObject (readonly)

Returns the value of attribute acts_like_form_tag


9
10
11
# File 'lib/bootstrap_form/form_builder.rb', line 9

def acts_like_form_tag
  @acts_like_form_tag
end

#control_colObject (readonly)

Returns the value of attribute control_col


9
10
11
# File 'lib/bootstrap_form/form_builder.rb', line 9

def control_col
  @control_col
end

#has_errorObject (readonly)

Returns the value of attribute has_error


9
10
11
# File 'lib/bootstrap_form/form_builder.rb', line 9

def has_error
  @has_error
end

#inline_errorsObject (readonly)

Returns the value of attribute inline_errors


9
10
11
# File 'lib/bootstrap_form/form_builder.rb', line 9

def inline_errors
  @inline_errors
end

#label_colObject (readonly)

Returns the value of attribute label_col


9
10
11
# File 'lib/bootstrap_form/form_builder.rb', line 9

def label_col
  @label_col
end

#label_errorsObject (readonly)

Returns the value of attribute label_errors


9
10
11
# File 'lib/bootstrap_form/form_builder.rb', line 9

def label_errors
  @label_errors
end

#layoutObject (readonly)

Returns the value of attribute layout


9
10
11
# File 'lib/bootstrap_form/form_builder.rb', line 9

def layout
  @layout
end

Instance Method Details

#check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_value = "0", &block) ⇒ Object


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/bootstrap_form/form_builder.rb', line 127

def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_value = "0", &block)
  options = options.symbolize_keys!
  check_box_options = options.except(:label, :label_class, :error_message, :help, :inline, :custom, :hide_label, :skip_label)
  check_box_classes = [check_box_options[:class]]
  check_box_classes << "position-static" if options[:skip_label] || options[:hide_label]
  check_box_classes << "is-invalid" if has_error?(name)
  if options[:custom]
    check_box_options[:class] = (["custom-control-input"] + check_box_classes).compact.join(' ')
  else
    check_box_options[:class] = (["form-check-input"] + check_box_classes).compact.join(' ')
  end

  checkbox_html = check_box_without_bootstrap(name, check_box_options, checked_value, unchecked_value)
  label_content = block_given? ? capture(&block) : options[:label]
  label_description = label_content || (object && object.class.human_attribute_name(name)) || name.to_s.humanize

  label_name = name
  # label's `for` attribute needs to match checkbox tag's id,
  # IE sanitized value, IE
  # https://github.com/rails/rails/blob/5-0-stable/actionview/lib/action_view/helpers/tags/base.rb#L123-L125
  if options[:multiple]
    label_name =
      "#{name}_#{checked_value.to_s.gsub(/\s/, "_").gsub(/[^-[[:word:]]]/, "").mb_chars.downcase.to_s}"
  end

  label_classes = [options[:label_class]]
  label_classes << hide_class if options[:hide_label]

  if options[:custom]
    div_class = ["custom-control", "custom-checkbox"]
    div_class.append("custom-control-inline") if layout_inline?(options[:inline])
    label_class = label_classes.prepend("custom-control-label").compact.join(" ")

    label_options = { class: label_class }
    label_options[:for] = options[:id] if options[:id].present?

    (:div, class: div_class.compact.join(" ")) do
      html = if options[:skip_label]
        checkbox_html
      else
        checkbox_html
          .concat(label(label_name, label_description, label_options))
      end
      html.concat(generate_error(name)) if options[:error_message]
      html
    end
  else
    wrapper_class = "form-check"
    wrapper_class += " form-check-inline" if layout_inline?(options[:inline])
    label_class = label_classes.prepend("form-check-label").compact.join(" ")

    label_options = { class: label_class }
    label_options[:for] = options[:id] if options[:id].present?

    (:div, class: wrapper_class) do
      html = if options[:skip_label]
        checkbox_html
      else
        checkbox_html
          .concat(label(label_name, label_description, label_options))
      end
      html.concat(generate_error(name)) if options[:error_message]
      html
    end
  end
end

#collection_check_boxes_with_bootstrap(*args) ⇒ Object


254
255
256
257
258
259
260
# File 'lib/bootstrap_form/form_builder.rb', line 254

def collection_check_boxes_with_bootstrap(*args)
  html = inputs_collection(*args) do |name, value, options|
    options[:multiple] = true
    check_box(name, options, value, nil)
  end
  hidden_field(args.first,{value: "", multiple: true}).concat(html)
end

#collection_radio_buttons_with_bootstrap(*args) ⇒ Object


264
265
266
267
268
# File 'lib/bootstrap_form/form_builder.rb', line 264

def collection_radio_buttons_with_bootstrap(*args)
  inputs_collection(*args) do |name, value, options|
    radio_button(name, value, options)
  end
end

#collection_select_with_bootstrap(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object


97
98
99
100
101
102
103
# File 'lib/bootstrap_form/form_builder.rb', line 97

def collection_select_with_bootstrap(method, collection, value_method, text_method, options = {}, html_options = {})
  form_group_builder(method, options, html_options) do
    input_with_error(method) do
      collection_select_without_bootstrap(method, collection, value_method, text_method, options, html_options)
    end
  end
end

#fields_for_with_bootstrap(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object


303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/bootstrap_form/form_builder.rb', line 303

def fields_for_with_bootstrap(record_name, record_object = nil, fields_options = {}, &block)
  if record_object.is_a?(Hash) && record_object.extractable_options?
    fields_options = record_object
    record_object = nil
  end
  fields_options[:layout] ||= options[:layout]
  fields_options[:label_col] = fields_options[:label_col].present? ? "#{fields_options[:label_col]}" : options[:label_col]
  fields_options[:control_col] ||= options[:control_col]
  fields_options[:inline_errors] ||= options[:inline_errors]
  fields_options[:label_errors] ||= options[:label_errors]
  fields_for_without_bootstrap(record_name, record_object, fields_options, &block)
end

#file_field_with_bootstrap(name, options = {}) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bootstrap_form/form_builder.rb', line 67

def file_field_with_bootstrap(name, options = {})
  options = options.reverse_merge(control_class: "custom-file-input")
  form_group_builder(name, options) do
    (:div, class: "custom-file") do
      input_with_error(name) do
        placeholder = options.delete(:placeholder) || "Choose file"
        placeholder_opts = { class: "custom-file-label" }
        placeholder_opts[:for] = options[:id] if options[:id].present?

        input = file_field_without_bootstrap(name, options)
        placeholder_label = label(name, placeholder, placeholder_opts)
        concat(input)
        concat(placeholder_label)
      end
    end
  end
end

#form_group(*args, &block) ⇒ Object


272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/bootstrap_form/form_builder.rb', line 272

def form_group(*args, &block)
  options = args.extract_options!
  name = args.first

  options[:class] = ["form-group", options[:class]].compact.join(' ')
  options[:class] << " row" if get_group_layout(options[:layout]) == :horizontal &&
                               !options[:class].split.include?("form-row")
  options[:class] << " form-inline" if field_inline_override?(options[:layout])
  options[:class] << " #{feedback_class}" if options[:icon]

  (:div, options.except(:append, :id, :label, :help, :icon, :input_group_class, :label_col, :control_col, :layout, :prepend)) do
    label = generate_label(options[:id], name, options[:label], options[:label_col], options[:layout]) if options[:label]
    control = capture(&block)

    help = options[:help]
    help_text = generate_help(name, help).to_s

    if get_group_layout(options[:layout]) == :horizontal
      control_class = options[:control_col] || control_col
      unless options[:label]
        control_offset = offset_col(options[:label_col] || @label_col)
        control_class = "#{control_class} #{control_offset}"
      end
      control = (:div, control + help_text, class: control_class)
      concat(label).concat(control)
    else
      concat(label).concat(control).concat(help_text)
    end
  end
end

#grouped_collection_select_with_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object


107
108
109
110
111
112
113
# File 'lib/bootstrap_form/form_builder.rb', line 107

def grouped_collection_select_with_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
  form_group_builder(method, options, html_options) do
    input_with_error(method) do
      grouped_collection_select_without_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
    end
  end
end

#radio_button_with_bootstrap(name, value, *args) ⇒ Object


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/bootstrap_form/form_builder.rb', line 196

def radio_button_with_bootstrap(name, value, *args)
  options = args.extract_options!.symbolize_keys!
  radio_options = options.except(:label, :label_class, :error_message, :help, :inline, :custom, :hide_label, :skip_label)
  radio_classes = [options[:class]]
  radio_classes << "position-static" if options[:skip_label] || options[:hide_label]
  radio_classes << "is-invalid" if has_error?(name)
  if options[:custom]
    radio_options[:class] = radio_classes.prepend("custom-control-input").compact.join(' ')
  else
    radio_options[:class] = radio_classes.prepend("form-check-input").compact.join(' ')
  end
  radio_html = radio_button_without_bootstrap(name, value, radio_options)

  disabled_class = " disabled" if options[:disabled]
  label_classes  = [options[:label_class]]
  label_classes << hide_class if options[:hide_label]

  if options[:custom]
    div_class = ["custom-control", "custom-radio"]
    div_class.append("custom-control-inline") if layout_inline?(options[:inline])
    label_class = label_classes.prepend("custom-control-label").compact.join(" ")

    label_options = { value: value, class: label_class }
    label_options[:for] = options[:id] if options[:id].present?

    (:div, class: div_class.compact.join(" ")) do
      html = if options[:skip_label]
        radio_html
      else
        radio_html
          .concat(label(name, options[:label], label_options))
      end
      html.concat(generate_error(name)) if options[:error_message]
      html
    end
  else
    wrapper_class = "form-check"
    wrapper_class += " form-check-inline" if layout_inline?(options[:inline])
    label_class = label_classes.prepend("form-check-label").compact.join(" ")

    label_options = { value: value, class: label_class }
    label_options[:for] = options[:id] if options[:id].present?

    (:div, class: "#{wrapper_class}#{disabled_class}") do
      html = if options[:skip_label]
        radio_html
      else
        radio_html
          .concat(label(name, options[:label], label_options))
      end
      html.concat(generate_error(name)) if options[:error_message]
      html
    end
  end
end

#select_with_bootstrap(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object


87
88
89
90
91
92
93
# File 'lib/bootstrap_form/form_builder.rb', line 87

def select_with_bootstrap(method, choices = nil, options = {}, html_options = {}, &block)
  form_group_builder(method, options, html_options) do
    prepend_and_append_input(method, options) do
      select_without_bootstrap(method, choices, options, html_options, &block)
    end
  end
end

#time_zone_select_with_bootstrap(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object


117
118
119
120
121
122
123
# File 'lib/bootstrap_form/form_builder.rb', line 117

def time_zone_select_with_bootstrap(method, priority_zones = nil, options = {}, html_options = {})
  form_group_builder(method, options, html_options) do
    input_with_error(method) do
      time_zone_select_without_bootstrap(method, priority_zones, options, html_options)
    end
  end
end