Class: BootstrapsBootstraps::BootstrapFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/bootstraps_bootstraps/bootstrap_form_builder.rb

Overview

This class started out as a gist somewhere but I can’t seem to find the

original.  At any rate its been modified over time to make it not break
and to update it to make it compatible with Bootstrap2.

If I'm being brutally honest its a bit of a cluster of metaprogramming
and it can be pretty difficult to understand.  BUT it does seem to work
so far.

Instance Method Summary collapse

Constructor Details

#initialize(object_name, object, template, options, block) ⇒ BootstrapFormBuilder

Returns a new instance of BootstrapFormBuilder.



30
31
32
33
34
35
36
37
38
39
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 30

def initialize(object_name, object, template, options, block)
  super(object_name, object, template, options, block)

  @form_mode = :vertical   #default value
  @form_mode = :horizontal if options[:horizontal] || detect_html_class(options, 'form-horizontal')
  @form_mode = :search     if options[:search]     || detect_html_class(options, 'form-search')
  @form_mode = :inline     if options[:inline]     || detect_html_class(options, 'form-inline')

  @action_wrapped = options[:action_wrapped]
end

Instance Method Details

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 81

def check_box method, options = {}, checked_value = 1, unchecked_value = 0
  if options[:vanilla]
    return super
  end

  guarantee_html_class options, :checkbox
  options[:class].push 'inline' if @form_mode == :inline

  text = options[:label] || ''
  options.delete :label

  #TODO provide support for grouped checkboxes
  div_with_class 'control-group' do
    div_with_class 'controls' do
      field_label(method, options) do
        super(method,options,checked_value,unchecked_value)  + text
      end
    end
  end
end

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 102

def collection_select method, collection, value_method, text_method, options = {}, html_options = {}
  #abort and just let the rails formbuilder do its thing
  if options[:vanilla]
    return super
  end

  errors, error_msg = render_errors method

  label_options = options.clone
  guarantee_html_class label_options, 'control-label'
  label = options[:label] == false ? ''.html_safe : field_label(method, label_options)

  guarantee_html_class options
  options[:class].push 'input-xlarge' if options[:large]
  options[:class].push 'inline' if @form_mode == :inline

  options.delete :label

  field = super(method,collection,value_method,text_method,options,html_options) + ' ' + error_msg

  #wrap it in div.controls
  field = div_with_class(['controls',errors], :content => field) if @form_mode == :horizontal

  #tack on the label
  field = label + field unless @form_mode == :inline

  #wrap it in div.control-group
  field = div_with_class(['control-group',errors], :content => field) if @form_mode == :horizontal

  field
end

#date_select(method, options = {}, html_options = {}) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 134

def date_select method, options = {}, html_options = {}
  field = super

  return field if options[:vanilla]

  errors, error_msg = render_errors method

  #hmm, apparently TODO
  help_text options

  label_options = html_options.clone
  label_options[:class] = ['control-label']
  #guarantee_html_class label_options, 'control-label'
  label = options[:label] == false ? ''.html_safe : field_label(method, label_options)

  guarantee_html_class html_options, 'inline'

  field += ' '.html_safe + error_msg
  field = div_with_class(['controls',errors], :content => field) if @form_mode == :horizontal
  field = label + field
  field = div_with_class(['control-group',errors], :content => field) if @form_mode == :horizontal
  field
end

#form_actions(field_options = {}, &block) ⇒ Object



202
203
204
205
206
207
208
209
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 202

def form_actions field_options = {}, &block
  field_options[:action_wrapped] = true
  wrapper = lambda { |content|
    field_group = div_with_class('form-actions', content: content)
  }

  @template.wrapped_inputs(@object_name, @object, @options.merge(field_options), wrapper, &block)
end

#grouped_inputs(field_options = {}, &block) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 190

def grouped_inputs field_options = {}, &block
  wrapper = lambda do |content|
    field_group = div_with_class('controls', content:content)
    if field_options[:label]
      field_group = label( field_options[:label], field_options[:label], class: 'control-label') + field_group
    end
    field_group = div_with_class('control-group', content:field_group)
  end

  @template.wrapped_inputs(@object_name, @object, @options, wrapper, &block)
end

#inline_inputs(field_options = {}, &block) ⇒ Object

input groups



180
181
182
183
184
185
186
187
188
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 180

def inline_inputs field_options = {}, &block
  field_options[:inline] = true
  wrapper = lambda { |content|
    field_group = div_with_class('controls', content: content)
    field_group = label( field_options[:label], field_options[:label], class: 'control-label' ) + field_group
    field_group = div_with_class('control-group', content: field_group)
  }
  @template.wrapped_inputs(@object_name, @object, @options.merge(field_options), wrapper, &block)
end

#radio_button(method, value, options = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 158

def radio_button method, value, options = {}
  guarantee_html_class options, 'radio'
  error_class, error_message = render_errors method

  text = options[:label] || ''
  options.delete :label

  (:label, class: options[:class]) do
    super(method,value,options) + text
  end
end

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



170
171
172
173
174
175
176
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 170

def submit text = nil, options = {}
  # options block gets stringify_keys called on it in the first super, no :keys exist after that
  field = super
  return field if options['vanilla']
  field = div_with_class('form-actions', content: field) unless options['no_action_block'] || @action_wrapped || [:inline, :search].include?(@form_mode)
  field
end