2009-11-18 23:50:43 +00:00
|
|
|
module SimpleForm
|
|
|
|
class FormBuilder < ActionView::Helpers::FormBuilder
|
2009-12-09 20:48:00 +00:00
|
|
|
# Components used by the folder builder.
|
|
|
|
# By default is [:label, :input, :hint, :error].
|
2009-12-09 19:41:20 +00:00
|
|
|
cattr_accessor :components, :instance_writer => false
|
2009-12-09 20:48:00 +00:00
|
|
|
@@components = [
|
|
|
|
SimpleForm::Components::Label, SimpleForm::Components::Input,
|
|
|
|
SimpleForm::Components::Hint, SimpleForm::Components::Error
|
|
|
|
]
|
2009-12-09 19:41:20 +00:00
|
|
|
|
2009-12-09 18:55:39 +00:00
|
|
|
# Make the template accessible for components
|
|
|
|
attr_reader :template
|
|
|
|
|
2009-11-19 21:26:16 +00:00
|
|
|
def input(attribute, options={})
|
2009-12-10 01:11:57 +00:00
|
|
|
input_type = default_input_type(attribute, options)
|
2009-12-08 13:48:31 +00:00
|
|
|
|
2009-12-09 19:41:20 +00:00
|
|
|
pieces = self.components.collect do |klass|
|
|
|
|
next if options[klass.basename] == false
|
|
|
|
klass.new(self, attribute, input_type, options).generate
|
|
|
|
end
|
2009-12-08 13:48:31 +00:00
|
|
|
|
2009-12-09 19:41:20 +00:00
|
|
|
pieces.compact.join
|
2009-11-19 21:26:16 +00:00
|
|
|
end
|
|
|
|
|
2009-12-08 16:49:14 +00:00
|
|
|
private
|
|
|
|
|
2009-12-10 01:11:57 +00:00
|
|
|
def default_input_type(attribute, options)
|
|
|
|
return options[:as].to_sym if options[:as]
|
|
|
|
return :select if options[:collection]
|
|
|
|
|
2009-12-09 19:41:20 +00:00
|
|
|
column = @object.column_for_attribute(attribute)
|
2009-12-09 13:12:23 +00:00
|
|
|
input_type = column.type
|
2009-12-10 00:55:42 +00:00
|
|
|
|
2009-12-08 16:49:14 +00:00
|
|
|
case input_type
|
2009-12-10 00:55:42 +00:00
|
|
|
when :timestamp
|
|
|
|
:datetime
|
|
|
|
when :string, nil
|
2009-12-09 19:41:20 +00:00
|
|
|
attribute.to_s =~ /password/ ? :password : :string
|
2009-12-10 00:55:42 +00:00
|
|
|
else
|
|
|
|
input_type
|
2009-12-08 16:49:14 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-08 19:08:36 +00:00
|
|
|
|
2009-11-18 23:50:43 +00:00
|
|
|
end
|
|
|
|
end
|