class Padrino::Helpers::FormBuilder::AbstractFormBuilder

def self.field_types

Returns the known field types for a formbuilder
def self.field_types
  [:hidden_field, :text_field, :text_area, :password_field, :file_field, :radio_button, :check_box, :select]
end

def build_object(object_or_symbol)

Returns a new record of the type specified in the object
explicit_object is either a symbol or a record
def build_object(object_or_symbol)
  object_or_symbol.is_a?(Symbol) ? object_class(object_or_symbol).new : object_or_symbol
end

def check_box(field, options={})

f.check_box :remember_me, :value => 'true', :uncheck_value => '0'
def check_box(field, options={})
  unchecked_value = options.delete(:uncheck_value) || '0'
  options.reverse_merge!(:id => field_id(field), :value => '1')
  options.merge!(:checked => true) if values_matches_field?(field, options[:value])
  html = hidden_field(field, :value => unchecked_value, :id => nil)
  html << @template.check_box_tag(field_name(field), options)
end

def error_messages(options={})

f.error_messages
def error_messages(options={})
  @template.error_messages_for(@object, options)
end

def field_error(field, options)

def field_error(field, options)
  if @object && @object.respond_to?(:errors) && @object.errors.respond_to?(:on) && @object.errors.on(field)
    options[:class] = ["x-form-text", "x-form-invalid", options[:class]].compact.join(" ")
  end
end

def field_human_name(field)

Returns the human name of the field. Look that use builtin I18n.
def field_human_name(field)
  I18n.translate("#{object_name}.#{field}", :count => 1, :default => field.to_s.humanize, :scope => [:model, :attributes])
end

def field_id(field, value=nil)

field_id(:gender, :male) => "user_gender_male"
field_id(:username) => "user_username"
Returns the id for the given field
def field_id(field, value=nil)
  value.blank? ? "#{object_name}_#{field}" : "#{object_name}_#{field}_#{value}"
end

def field_name(field)

field_name(:username) => "user[username]"
Returns the name for the given field
def field_name(field)
  "#{object_name}[#{field}]"
end

def field_value(field)

field_value(:username) => "Joey"
Returns the value for the object's field
def field_value(field)
  @object && @object.respond_to?(field) ? @object.send(field) : ""
end

def file_field(field, options={})

f.file_field :photo, :class => 'avatar'
def file_field(field, options={})
  options.reverse_merge!(:id => field_id(field), :class => field_error(field, options))
  @template.file_field_tag field_name(field), options
end

def hidden_field(field, options={})

f.hidden_field :session_id, :value => "45"
def hidden_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  @template.hidden_field_tag field_name(field), options
end

def image_submit(source, options={})

f.simage_submitubmit "buttons/submit.png", :class => 'large'
def image_submit(source, options={})
  @template.image_submit_tag source, options
end

def initialize(template, object)

def initialize(template, object)
  @template = template
  @object   = build_object(object)
  raise "FormBuilder template must be initialized!" unless template
  raise "FormBuilder object must be not be nil value. If there's no object, use a symbol instead! (i.e :user)" unless object
end

def label(field, options={})

f.label :username, :caption => "Nickname"
def label(field, options={})
  options.reverse_merge!(:caption => "#{field_human_name(field)}: ")
  @template.label_tag(field_id(field), options)
end

def object_class(explicit_object)

Returns the class type for the given object
def object_class(explicit_object)
  explicit_object.is_a?(Symbol) ? explicit_object.to_s.classify.constantize : explicit_object.class
end

def object_name

=> user_assignment
Returns the object's models name
def object_name
  object.is_a?(Symbol) ? object : object.class.to_s.underscore.gsub('/', '-')
end

def password_field(field, options={})

f.password_field :password, :id => 'password'
def password_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field), :class => field_error(field, options))
  @template.password_field_tag field_name(field), options
end

def radio_button(field, options={})

f.radio_button :gender, :value => 'male'
def radio_button(field, options={})
  options.reverse_merge!(:id => field_id(field, options[:value]))
  options.merge!(:checked => true) if values_matches_field?(field, options[:value])
  @template.radio_button_tag field_name(field), options
end

def select(field, options={})

f.select :color, :collection => @colors, :fields => [:name, :id]
f.select :color, :options => ['red', 'green'], :include_blank => true
def select(field, options={})
  options.reverse_merge!(:id => field_id(field), :selected => field_value(field), :class => field_error(field, options))
  @template.select_tag field_name(field), options
end

def submit(caption="Submit", options={})

f.submit "Update", :class => 'large'
def submit(caption="Submit", options={})
  @template.submit_tag caption, options
end

def text_area(field, options={})

f.text_area :summary, :value => "(enter summary)", :id => 'summary'
def text_area(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field), :class => field_error(field, options))
  @template.text_area_tag field_name(field), options
end

def text_field(field, options={})

f.text_field :username, :value => "(blank)", :id => 'username'
def text_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field), :class => field_error(field, options))
  @template.text_field_tag field_name(field), options
end

def values_matches_field?(field, value)

field_has_value?(:gender, 'male')
Returns true if the value matches the value in the field
def values_matches_field?(field, value)
  value.present? && (field_value(field).to_s == value.to_s || field_value(field).to_s == 'true')
end