1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

Improve mapping readability and add select example

This commit is contained in:
José Valim 2009-12-09 13:57:15 -02:00
parent 286a115d44
commit 4320fb614f
3 changed files with 29 additions and 18 deletions

View file

@ -12,7 +12,7 @@ module SimpleForm
def input(attribute, options={})
@attribute, @options = attribute, options
@options.assert_valid_keys(:as, :label, :required, :hint, :options, :html)
@options.assert_valid_keys(:as, :label, :required, :hint, :options, :html, :collection)
@input_type = (@options[:as] || default_input_type).to_sym

View file

@ -1,19 +1,20 @@
module SimpleForm
module Input
Mapping = Struct.new(:method, :arity)
Mapping = Struct.new(:method, :collection, :options)
MAPPINGS = {
:boolean => Mapping.new(:check_box, 2),
:text => Mapping.new(:text_area, 2),
:datetime => Mapping.new(:datetime_select, 3),
:date => Mapping.new(:date_select, 3),
:time => Mapping.new(:time_select, 3),
:password => Mapping.new(:password_field, 2),
:hidden => Mapping.new(:hidden_field, 2),
:boolean => Mapping.new(:check_box, false, false),
:text => Mapping.new(:text_area, false, false),
:datetime => Mapping.new(:datetime_select, false, true),
:date => Mapping.new(:date_select, false, true),
:time => Mapping.new(:time_select, false, true),
:password => Mapping.new(:password_field, false, false),
:hidden => Mapping.new(:hidden_field, false, false),
:select => Mapping.new(:select, true, true),
# Do we need integer and numeric?
:integer => Mapping.new(:text_field, 2),
:numeric => Mapping.new(:text_field, 2),
:string => Mapping.new(:text_field, 2)
:integer => Mapping.new(:text_field, false, false),
:numeric => Mapping.new(:text_field, false, false),
:string => Mapping.new(:text_field, false, false)
}
private
@ -34,12 +35,12 @@ module SimpleForm
mapping = MAPPINGS[@input_type]
raise "Invalid input type #{@input_type.inspect}" unless mapping
case mapping.arity
when 3
send(mapping.method, @attribute, @options[:options], html_options)
when 2
send(mapping.method, @attribute, html_options)
end
args = [ @attribute ]
args << @options[:collection] if mapping.collection
args << @options[:options] if mapping.options
args << html_options
send(mapping.method, *args)
end
def boolean_collection

View file

@ -179,4 +179,14 @@ class InputTest < ActionView::TestCase
end
assert_no_select 'form input.required'
end
test 'input requires a collection for select types' do
simple_form_for @user do |f|
concat f.input :name, :as => :select, :collection => [ "Jose", "Carlos"]
end
assert_select 'form select.required'
assert_select 'form select option', 'Jose'
assert_select 'form select option', 'Carlos'
end
end