Class: BootstrapFormHelper::FormBuilder

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

Constant Summary collapse

INPUTS =
(%w(select collection_select grouped_collection_select time_zone_select).map(&:to_sym) +
field_helpers -
%w(label check_box radio_button fields_for hidden_field).map(&:to_sym))

Instance Method Summary collapse

Instance Method Details

#actions(&block) ⇒ Object

Wraps action buttons into their own styled container.



24
25
26
# File 'lib/bootstrap_form_helper/form_builder.rb', line 24

def actions(&block)
  @template.(:div, class: 'form-actions', &block)
end

#button(value = nil, options = {}) ⇒ Object

Renders a button with default classes to style it as a form button.



69
70
71
# File 'lib/bootstrap_form_helper/form_builder.rb', line 69

def button(value = nil, options = {})
  super value, { type: 'button', class: 'btn' }.merge(options)
end

#fieldset(legend = nil, options = {}) ⇒ Object

Wraps the contents of the block passed to the method in a fieldset tag with a optional legend text.



14
15
16
17
18
19
# File 'lib/bootstrap_form_helper/form_builder.rb', line 14

def fieldset(legend = nil, options = {})
  @template.(:fieldset, options) do
    @template.concat @template.(:legend, legend) unless legend.nil?
    yield
  end
end

#label(attribute, text = '', options = {}, &block) ⇒ Object

Attaches a label to the inputs rendered inside of the block passed to it. Associates the label with the input for the attribute given. If text is passed, uses that as the text for the label; otherwise humanizes the attribute name.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bootstrap_form_helper/form_builder.rb', line 45

def label(attribute, text = '', options = {}, &block)
  text, attribute = attribute, nil if attribute.kind_of? String

  options = { class: 'control-label' }.merge(options)
  id      = wrapper_id      attribute, 'control_group'
  classes = wrapper_classes attribute, 'control-group'
  fields_options = options.merge(builder: BootstrapFormHelper::FormControls)

  @template.(:div, id: id, class: classes) do
    @template.concat case
      when attribute && text then super(attribute, text, options, &nil)
      when attribute         then super(attribute, nil,  options, &nil)
      when text              then @template.label_tag(nil, text, options, &nil)
    end

    @template.concat @template.(:div, class: 'controls') {
      @template.fields_for object_name, object, fields_options, &block
      }
  end
end

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

Renders a submit tag with default classes to style it as a primary form button.



77
78
79
# File 'lib/bootstrap_form_helper/form_builder.rb', line 77

def submit(value = nil, options = {})
  button value, { type: 'submit', class: 'btn btn-primary' }.merge(options)
end