2009-11-18 18:50:43 -05:00
|
|
|
module SimpleForm
|
|
|
|
class FormBuilder < ActionView::Helpers::FormBuilder
|
2009-12-10 08:01:44 -05:00
|
|
|
attr_reader :template, :object_name, :object, :attribute, :input_type, :options
|
2009-12-09 13:55:39 -05:00
|
|
|
|
2009-11-19 16:26:16 -05:00
|
|
|
def input(attribute, options={})
|
2009-12-10 08:01:44 -05:00
|
|
|
@attribute, @options = attribute, options
|
|
|
|
@input_type = default_input_type
|
2009-12-08 08:48:31 -05:00
|
|
|
|
2009-12-10 08:01:44 -05:00
|
|
|
component = SimpleForm.terminator
|
|
|
|
SimpleForm.components.reverse.each do |klass|
|
|
|
|
next if @options[klass.basename] == false
|
|
|
|
component = klass.new(self, component)
|
2009-12-09 14:41:20 -05:00
|
|
|
end
|
2009-12-08 08:48:31 -05:00
|
|
|
|
2009-12-10 08:01:44 -05:00
|
|
|
component.call
|
2009-11-19 16:26:16 -05:00
|
|
|
end
|
|
|
|
|
2009-12-09 20:57:05 -05:00
|
|
|
private
|
2009-12-08 11:49:14 -05:00
|
|
|
|
2009-12-10 08:47:07 -05:00
|
|
|
def column
|
|
|
|
@object.column_for_attribute(@attribute) if @object.respond_to?(:column_for_attribute)
|
|
|
|
end
|
|
|
|
|
2009-12-10 08:01:44 -05:00
|
|
|
def default_input_type
|
|
|
|
return @options[:as].to_sym if @options[:as]
|
|
|
|
return :select if @options[:collection]
|
2009-12-09 20:11:57 -05:00
|
|
|
|
2009-12-10 08:47:07 -05:00
|
|
|
input_type = column.try(:type)
|
2009-12-09 19:55:42 -05:00
|
|
|
|
2009-12-09 20:57:05 -05:00
|
|
|
case input_type
|
|
|
|
when :timestamp
|
|
|
|
:datetime
|
|
|
|
when :string, nil
|
2009-12-10 08:23:48 -05:00
|
|
|
@attribute.to_s =~ /password/ ? :password : :string
|
2009-12-09 20:57:05 -05:00
|
|
|
else
|
|
|
|
input_type
|
2009-12-08 11:49:14 -05:00
|
|
|
end
|
2009-12-09 20:57:05 -05:00
|
|
|
end
|
2009-12-08 14:08:36 -05:00
|
|
|
|
2009-11-18 18:50:43 -05:00
|
|
|
end
|
|
|
|
end
|