Class: TwitterBootstrapFormalwear::FormBuilder

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

Constant Summary collapse

INPUTS =
[
  :check_box,
  :radio_button,
  :select,
  *ActionView::Helpers::FormBuilder.instance_methods.grep(%r{
    _(area|field|select)$ # all area, field, and select methods
  }mx).map(&:to_sym)
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



8
9
10
# File 'lib/twitter_bootstrap_formalwear/form_builder.rb', line 8

def object
  @object
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



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

def object_name
  @object_name
end

#templateObject (readonly)

Returns the value of attribute template.



7
8
9
# File 'lib/twitter_bootstrap_formalwear/form_builder.rb', line 7

def template
  @template
end

Instance Method Details

#actions(&block) ⇒ Object

Wraps action buttons into their own styled container.



41
42
43
# File 'lib/twitter_bootstrap_formalwear/form_builder.rb', line 41

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.



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

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

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

Generates a controls div element for the given attribute.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/twitter_bootstrap_formalwear/form_builder.rb', line 82

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

  template.(:div, :class => 'controls') {
    template.fields_for(
      self.object_name,
      self.object,
      self.options.merge(:builder => TwitterBootstrapFormalwear::FormControls),
      &block
    )
  }
end

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

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



31
32
33
34
35
36
# File 'lib/twitter_bootstrap_formalwear/form_builder.rb', line 31

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

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

Generates a control-group div element for the given attribute, containing label and controls elements.



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/twitter_bootstrap_formalwear/form_builder.rb', line 49

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

  id      = _wrapper_id      attribute, 'control_group'
  classes = _wrapper_classes attribute, 'control-group'

  template.(:div, :id => id, :class => classes) do
    if !attribute.blank?
      template.concat self.label(attribute, text, options, &block)
    end
    template.concat self.controls(attribute, text, options, &block)
  end
end

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

Generates a label element for the given attribute. If text is passed, uses that as the text for the label; otherwise humanizes the attribute name.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/twitter_bootstrap_formalwear/form_builder.rb', line 67

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

  options = options.merge(:class => 'control-label')

  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
end

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

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



109
110
111
112
113
114
# File 'lib/twitter_bootstrap_formalwear/form_builder.rb', line 109

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