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


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/formatted_form/form_builder.rb', line 37

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  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           = nil           if !is_array
    [super(method, options, checked, unchecked), label, options[:class].to_s =~ /inline/]
  end
  default_field(:check_box, method, options.merge(:choices => choices))
end

#element(label = false, value = nil, &block) ⇒ Object

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

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


83
84
85
86
# File 'lib/formatted_form/form_builder.rb', line 83

def element(label = false, value = nil, &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



89
90
91
92
# File 'lib/formatted_form/form_builder.rb', line 89

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


54
55
56
57
58
59
60
61
# File 'lib/formatted_form/form_builder.rb', line 54

def radio_button(method, tag_value, options = {})
  tag_values = tag_value.is_a?(Array) ? tag_value : [tag_value]
  choices = tag_values.collect do |label, choice|
    label, choice = label, label if !choice
    [super(method, choice, options), label, options[:class].to_s =~ /inline/]
  end
  default_field(:radio_button, method, options.merge(:choices => choices))
end

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

Wrapper for the select field



26
27
28
29
30
# File 'lib/formatted_form/form_builder.rb', line 26

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'


65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/formatted_form/form_builder.rb', line 65

def submit(value = nil, options = {}, &block)
  value, options = nil, value if value.is_a?(Hash)
  value ||= submit_default_value
  
  # Add specific bootstrap class
  options[:class] = "#{options[:class]} btn".strip
  options[:class] = "#{options[:class]} btn-primary" unless options[:class] =~ /btn-/
  
  default_field(:submit, nil, options) do
    super(value, options)
  end
end