Class: BootstrapForm::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/bootstrap_form/form_builder.rb

Constant Summary collapse

FIELD_HELPERS =
%w[
  color_field date_field datetime_field email_field file_field month_field
  number_field password_field phone_field range_field search_field text_area
  text_field time_field url_field week_field
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FormBuilder.



17
18
19
20
# File 'lib/bootstrap_form/form_builder.rb', line 17

def initialize(object_name, object, template, options)
  @form_bootstrap = BootstrapForm::BootstrapOptions.new(options.delete(:bootstrap))
  super(object_name, object, template, options)
end

Instance Attribute Details

#form_bootstrapObject

Bootstrap settings set on the form itself



15
16
17
# File 'lib/bootstrap_form/form_builder.rb', line 15

def form_bootstrap
  @form_bootstrap
end

Instance Method Details

#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object

Wrapper around checkbox. Example usage:

checkbox :agree, bootstrap: {label: {text: "Do you agree?"}}


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bootstrap_form/form_builder.rb', line 56

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  bootstrap = form_bootstrap.scoped(options.delete(:bootstrap))

  help_text = draw_help(bootstrap.help)
  errors    = draw_errors(method)

  add_css_class!(options, "form-check-input")
  add_css_class!(options, "is-invalid") if errors.present?

  label_text = nil
  if (custom_text = bootstrap.label[:text]).present?
    label_text = custom_text
  end

  fieldset_css_class = "form-group"
  fieldset_css_class << " row" if bootstrap.horizontal?
  fieldset_css_class << " #{bootstrap.inline_margin_class}" if bootstrap.inline?

  (:fieldset, class: fieldset_css_class) do
    draw_control_column(bootstrap, offset: true) do
      (:div, class: "form-check") do
        concat super(method, options, checked_value, unchecked_value)
        concat label(method, label_text, class: "form-check-label")
        concat errors     if errors.present?
        concat help_text  if help_text.present?
      end
    end
  end
end

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

Helper to generate multiple checkboxes. Same options as for radio buttons. Example usage:

collection_check_boxes :choices, Choice.all, :id, :label


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/bootstrap_form/form_builder.rb', line 111

def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {})
  bootstrap = form_bootstrap.scoped(options.delete(:bootstrap))

  content = "".html_safe
  unless options[:include_hidden] == false
    content << hidden_field(method, multiple: true, value: "")
  end

  args = [bootstrap, method, collection, value_method, text_method, options, html_options]
  content << draw_choices(*args) do |m, v, opts|
    opts[:multiple]       = true
    opts[:include_hidden] = false
    ActionView::Helpers::FormBuilder.instance_method(:check_box).bind(self).call(m, opts, v)
  end
end

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

Helper to generate multiple radio buttons. Example usage:

collection_radio_buttons :choices, ["a", "b"], :to_s, :to_s %>
collection_radio_buttons :choices, [["a", "Label A"], ["b", "Label B"]], :first, :second
collection_radio_buttons :choices, Choice.all, :id, :label

Takes bootstrap options:

inline: true            - to render inputs inline
label: {text: "Custom"} - to specify a label
label: {hide: true}     - to not render label at all


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

def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {})
  bootstrap = form_bootstrap.scoped(options.delete(:bootstrap))

  args = [bootstrap, method, collection, value_method, text_method, options, html_options]
  draw_choices(*args) do |m, v, opts|
    radio_button(m, v, opts)
  end
end

#form_group(options = {}) ⇒ Object

Helper method to put arbitrary content in markup that renders correctly for the Bootstrap form. Example:

form_group bootstrap: {label: {text: "Label"}} do
  "Some content"
end


188
189
190
191
192
193
194
195
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
# File 'lib/bootstrap_form/form_builder.rb', line 188

def form_group(options = {})
  bootstrap = form_bootstrap.scoped(options.delete(:bootstrap))

  label_text = bootstrap.label[:text]

  label =
    if label_text.present?
      label_options = {}
      add_css_class!(label_options, bootstrap.label[:class])

      if bootstrap.horizontal?
        add_css_class!(label_options, "col-form-label")
        add_css_class!(label_options, bootstrap.label_col_class)
        add_css_class!(label_options, bootstrap.label_align_class)
      elsif bootstrap.inline?
        add_css_class!(label_options, bootstrap.inline_margin_class)
      end

      (:label, label_text, label_options)
    end

  form_group_class = "form-group"
  form_group_class << " row"      if bootstrap.horizontal?
  form_group_class << " mr-sm-2"  if bootstrap.inline?

  (:div, class: form_group_class) do
    content = "".html_safe
    content << label if label.present?
    content << draw_control_column(bootstrap, offset: label.blank?) do
      yield
    end
  end
end

#plaintext(method, options = {}) ⇒ Object

Bootstrap wrapper for readonly text field that is shown as plain text.

plaintext(:value)


131
132
133
134
135
136
137
138
139
# File 'lib/bootstrap_form/form_builder.rb', line 131

def plaintext(method, options = {})
  bootstrap = form_bootstrap.scoped(options.delete(:bootstrap))
  draw_form_group(bootstrap, method, options) do
    remove_css_class!(options, "form-control")
    add_css_class!(options, "form-control-plaintext")
    options[:readonly] = true
    ActionView::Helpers::FormBuilder.instance_method(:text_field).bind(self).call(method, options)
  end
end

#primary(value = nil, options = {}, &block) ⇒ Object

Same as submit button, only with btn-primary class added



176
177
178
179
# File 'lib/bootstrap_form/form_builder.rb', line 176

def primary(value = nil, options = {}, &block)
  add_css_class!(options, "btn-primary")
  submit(value, options, &block)
end

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

Wrapper for select helper. Boostrap options are sent via html_options hash:

select :choices, ["a", "b"], {}, bootstrap: {label: {text: "Custom"}}


45
46
47
48
49
50
# File 'lib/bootstrap_form/form_builder.rb', line 45

def select(method, choices = nil, options = {}, html_options = {}, &block)
  bootstrap = form_bootstrap.scoped(options.delete(:bootstrap))
  draw_form_group(bootstrap, method, html_options) do
    super(method, choices, options, html_options, &block)
  end
end

#submit(value = nil, options = {}, &block) ⇒ Object

Add bootstrap formatted submit button. If you need to change its type or add another css class, you need to override all css classes like so:

submit(class: "btn btn-info custom-class")

You may add additional content that directly follows the button. Here’s an example of a cancel link:

submit do
  link_to("Cancel", "/", class: "btn btn-link")
end


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/bootstrap_form/form_builder.rb', line 153

def submit(value = nil, options = {}, &block)
  if value.is_a?(Hash)
    options = value
    value   = nil
  end

  bootstrap = form_bootstrap.scoped(options.delete(:bootstrap))

  add_css_class!(options, "btn")

  form_group_class = "form-group"
  form_group_class << " row" if bootstrap.horizontal?

  (:div, class: form_group_class) do
    draw_control_column(bootstrap, offset: true) do
      out = super(value, options)
      out << capture(&block) if block_given?
      out
    end
  end
end