heartcombo--simple_form/lib/simple_form/form_builder.rb

50 lines
1.2 KiB
Ruby
Raw Normal View History

require 'simple_form/label'
require 'simple_form/input'
require 'simple_form/hint'
2009-12-08 13:38:03 -05:00
require 'simple_form/error'
2009-11-18 18:50:43 -05:00
module SimpleForm
class FormBuilder < ActionView::Helpers::FormBuilder
include SimpleForm::Label
include SimpleForm::Input
include SimpleForm::Hint
2009-12-08 13:38:03 -05:00
include SimpleForm::Error
2009-11-19 16:26:16 -05:00
def input(attribute, options={})
2009-12-08 09:55:04 -05:00
@attribute, @options = attribute, options
@options.assert_valid_keys(:as, :label, :required, :hint, :options, :html)
@input_type = (@options[:as] || default_input_type).to_sym
2009-12-08 08:48:31 -05:00
2009-12-08 09:55:04 -05:00
label = generate_label
input = generate_input
hint = generate_hint
2009-12-08 13:38:03 -05:00
error = generate_error
2009-12-08 08:48:31 -05:00
2009-12-08 13:38:03 -05:00
label << input << hint << error
2009-11-19 16:26:16 -05:00
end
private
def required_class
'required' if attribute_required?
end
def attribute_required?
true unless @options[:required] == false
end
def default_input_type
input_type = @object.try(:column_for_attribute, @attribute)
case input_type
when nil then :string
when :decimal then :numeric
when :timestamp then :datetime
when :string then
@attribute.to_s =~ /password/ ? :password : :string
else input_type
end
end
2009-11-18 18:50:43 -05:00
end
end