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

44 lines
1.1 KiB
Ruby
Raw Normal View History

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