2009-12-08 10:16:46 -05:00
|
|
|
require 'simple_form/label'
|
2009-12-08 10:44:54 -05:00
|
|
|
require 'simple_form/input'
|
2009-12-08 12:59:50 -05:00
|
|
|
require 'simple_form/hint'
|
2009-12-08 13:38:03 -05:00
|
|
|
require 'simple_form/error'
|
2009-12-08 10:16:46 -05:00
|
|
|
|
2009-11-18 18:50:43 -05:00
|
|
|
module SimpleForm
|
|
|
|
class FormBuilder < ActionView::Helpers::FormBuilder
|
2009-12-08 10:16:46 -05:00
|
|
|
include SimpleForm::Label
|
2009-12-08 10:44:54 -05:00
|
|
|
include SimpleForm::Input
|
2009-12-08 12:59:50 -05:00
|
|
|
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
|
2009-12-08 12:59:50 -05:00
|
|
|
@options.assert_valid_keys(:as, :label, :required, :hint, :options, :html)
|
2009-12-08 11:49:14 -05:00
|
|
|
|
|
|
|
@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
|
2009-12-08 12:59:50 -05:00
|
|
|
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
|
|
|
|
|
2009-12-08 11:49:14 -05:00
|
|
|
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
|