class FormattedForm::FormBuilder

Public Instance Methods

check_box(method, options = {}, checked_value = "1", unchecked_value = "0") click to toggle source

Same as the old check box but it’s possible to send an array of values

form.check_box :color
form.check_box :color, {}, 'white'
form.check_box :color, {}, ['red', 'blue']
form.check_box :color, {}, [['Red', 'red'], ['Blue', 'blue']]
Calls superclass method
# File lib/formatted_form/form_builder.rb, line 47
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  return super(method, options, checked_value, unchecked_value) if options.delete(:builder) == false
  is_array = checked_value.is_a?(Array)
  options.merge!(:multiple => true) if is_array
  checked_value = is_array ? checked_value : [[checked_value, checked_value, unchecked_value]]
  choices = checked_value.collect do |label, checked, unchecked|
    label, checked  = label, label          if !checked && is_array
    checked         = label                 if !checked
    label           = method.to_s.humanize  if !is_array
    
    match = super(method, options, checked, unchecked).match(/(<input .*?\/>)?(<input .*?\/>)/)
    hidden, input = match[1], match[2]
    [hidden.to_s.html_safe, input.to_s.html_safe, label]
  end
  default_field(:check_box, method, options.merge(:choices => choices))
end
element(label = false, value = nil, options = {}, &block) click to toggle source

Form helper to render generic content as a form field. For example:

form.element 'Label', 'Content'
form.element 'Label do
  Content
end
# File lib/formatted_form/form_builder.rb, line 91
def element(label = false, value = nil, options = {}, &block)
  options = {:label => label, :content => value }
  default_field(:element, nil, options, &block)
end
fields_for(record_name, record_object = nil, options = {}, &block) click to toggle source

adding builder class for fields_for

Calls superclass method
# File lib/formatted_form/form_builder.rb, line 97
def fields_for(record_name, record_object = nil, options = {}, &block)
  options[:builder] ||= FormattedForm::FormBuilder
  super(record_name, record_object, options, &block)
end
radio_button(method, tag_value, options = {}) click to toggle source

Radio button helper. Optionally it’s possible to specify multiple choices at once:

form.radio_button :role, 'admin'
form.radio_button :role, ['admin', 'regular']
form.radio_button :role, [['Admin', 1], ['Regular', 0]]
Calls superclass method
# File lib/formatted_form/form_builder.rb, line 68
def radio_button(method, tag_value, options = {})
  return super(method, tag_value, options) if options.delete(:builder) == false
  tag_values = tag_value.is_a?(Array) ? tag_value : [tag_value]
  choices = tag_values.collect do |label, choice|
    label, choice = label, label if choice.nil?
    [nil, super(method, choice, options), label]
  end
  default_field(:radio_button, method, options.merge(:choices => choices))
end
select(method, choices, options = {}, html_options = {}) click to toggle source

Wrapper for the select field

Calls superclass method
# File lib/formatted_form/form_builder.rb, line 36
def select(method, choices, options = {}, html_options = {})
  default_field(:select, method, options) do 
    super(method, choices, options, html_options)
  end 
end
submit(value = nil, options = {}, &block) click to toggle source

Creates submit button element with appropriate bootstrap classes.

form.submit, :class => 'btn-danger'
Calls superclass method
# File lib/formatted_form/form_builder.rb, line 80
def submit(value = nil, options = {}, &block)
  default_field(:submit, nil, options) do
    super(value, options)
  end
end

Protected Instance Methods

builder_options!(options = {}) click to toggle source

Extacts parameters that are used for rendering the field

# File lib/formatted_form/form_builder.rb, line 122
def builder_options!(options = {})
  [
    :label, :prepend, :append, :prepend_html, :append_html, :help_block, :help_inline, :choices, :inline
  ].each_with_object({}) do |attr, hash|
    hash[attr] = options.delete(attr)
  end
end
default_field(field_name, method, options = nil) { || ... } click to toggle source

Main rendering method

# File lib/formatted_form/form_builder.rb, line 105
def default_field(field_name, method, options = nil, &block)
  options ||= {}
  return yield if options.delete(:builder) == false && block_given?
  builder_options = builder_options!(options)
  @template.render(
    :partial => "formatted_form/#{field_name}",
    :locals  => { :options => builder_options.merge(
      :builder    => self,
      :field_name => field_name,
      :method     => method,
      :content    => block_given?? @template.capture(&block) : options.delete(:content),
      :errors     => method ? error_messages_for(method) : nil
    )}
  )
end
error_messages_for(method) click to toggle source

Collecting errors for a field and outputting first instance

# File lib/formatted_form/form_builder.rb, line 131
def error_messages_for(method)
  [object.errors[method]].flatten.first if object && object.respond_to?(:errors)
end