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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 80

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

  field_label(method, options) do
    super(method,options,checked_value,unchecked_value)  + text
  end
end

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 96

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[:label] == false ? ''.html_safe : field_label(method, html_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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 126

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[:label] == false ? ''.html_safe : field_label(method, options.except(:class))

  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



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

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



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

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



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

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



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/bootstraps_bootstraps/bootstrap_form_builder.rb', line 147

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(method, options = {}) ⇒ Object



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

def submit method, 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)
  puts @action_wrapped
  field
end