Class: FormattedForm::FormBuilder

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

Instance Method Summary collapse

Instance Method Details

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

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']]


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object

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

form.element 'Label', 'Content'
form.element 'Label do
  Content
end


91
92
93
94
# 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) ⇒ Object

adding builder class for fields_for



97
98
99
100
# 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 = {}) ⇒ Object

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]]


68
69
70
71
72
73
74
75
76
# 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 = {}) ⇒ Object

Wrapper for the select field



36
37
38
39
40
# 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) ⇒ Object

Creates submit button element with appropriate bootstrap classes.

form.submit, :class => 'btn-danger'


80
81
82
83
84
# 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