Class: MNE::TwitterBootstrapFormBuilder::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::DynamicForm::FormBuilderMethods
Defined in:
lib/twitter-bootstrap-form-builder/form_builder.rb

Instance Method Summary collapse

Instance Method Details

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

Special handling for check boxes, which can have two labels

Options

:label - As the *_field methods
:text - Like :label but for the right-hand label


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/twitter-bootstrap-form-builder/form_builder.rb', line 66

def check_box(field, options = {}, checked_value = "1", unchecked_value = "0")
  label_tag = build_label_tag(field, options)

  return super if label_tag === false

  control_group_opts = options[:control_group] || {}

  options[:text] ||= false

  control_group(field, control_group_opts) do
    label_tag + @template.(:div, :class => "controls") do

      opts = options.clone
      opts[:text] = nil
      check_box_tag = super(field, opts, checked_value, unchecked_value).html_safe

      text_tag = build_label_tag(field, options, :text, {:class => "checkbox"}) do |opts|
        "#{check_box_tag} #{opts[0]}".html_safe
      end

      text_tag ||= @template.(:label, check_box_tag, :class => "checkbox")

      text_tag.html_safe + errors_for(field)
    end.html_safe
  end.html_safe
end

#control_group(field = nil, opts = {}, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/twitter-bootstrap-form-builder/form_builder.rb', line 9

def control_group(field = nil, opts = {}, &block)
  raise "Expected hash for options, got #{opts.inspect}" unless opts.is_a? Hash

  # wrap in array if not an array
  opts[:class] = Array(opts[:class])
  opts[:class] << "control-group"
  opts[:class] << field.to_s.gsub(/[^-_a-zA-Z0-9]/, "") if field

  opts[:class] << "error" if @object && @object.errors.messages.has_key?(field) && @object.errors.messages[field].any?

  @template.(:div, opts) do
    @template.capture &block
  end
end

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

Very light wrapper around FormBuilder#submit which adds a ‘btn’ class

Options:

:label => false - fall back to FormBuilder#submit
:primary => true - add btn-primary class


98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/twitter-bootstrap-form-builder/form_builder.rb', line 98

def submit(value = nil, options = {})

  if options[:label] === false
    options.delete(:label)
    return super
  end

  options[:class] = "#{options[:class]} btn"
  options[:class] = "#{options[:class]} btn-primary" if options.delete(:primary)

  super(value, options)
end