:include_blank is added by default on select.

This commit is contained in:
José Valim 2009-12-09 23:34:15 -02:00
parent 728dd5ba45
commit 2881e775c6
3 changed files with 36 additions and 20 deletions

View File

@ -5,13 +5,12 @@
* Sample CSS
* Test forms with non objects
* Get default string options from column definition
* Improve input_type heuristic
* Detect label and values automatically
* Add support to default :include_blank
* Add support to default :prompt methods on datetime inputs
* Add support to default label method
* Add wrapper support
* Improve readme with examples
* :country, :time_zone and :file support
* :country, :time_zone, :group and :file types support
== Associations

View File

@ -35,34 +35,41 @@ module SimpleForm
raise "Invalid input type #{@input_type.inspect}" unless mapping
args = [ @attribute ]
if mapping.collection
collection = (@options[:collection] || self.class.boolean_collection).to_a
detect_collection_methods(collection, @options)
args.push(collection, @options[:value_method], @options[:label_method])
end
args << @options[:options] if mapping.options
apply_collection_behavior(args) if mapping.collection
apply_options_behavior(args) if mapping.options
args << html_options
@builder.send(mapping.method, *args)
end
protected
def apply_collection_behavior(args)
collection = (@options[:collection] || self.class.boolean_collection).to_a
detect_collection_methods(collection, @options)
@options[:options][:include_blank] = true unless @options[:options].key?(:include_blank)
args.push(collection, @options[:value_method], @options[:label_method])
end
def apply_options_behavior(args)
args << @options[:options]
end
def detect_collection_methods(collection, options)
case collection.first
when Array
options[:label_method] ||= :first
options[:value_method] ||= :last
when String
options[:label_method] ||= :to_s
options[:value_method] ||= :to_s
label, value = :first, :last
when Integer
options[:label_method] ||= :to_s
options[:value_method] ||= :to_i
value = :to_i
when String
# Do nothing ...
else
options[:label_method] ||= :to_s
options[:value_method] ||= :to_s
# TODO Implement detection logic
end
options[:label_method] ||= label || :to_s
options[:value_method] ||= value || :to_s
end
end
end

View File

@ -180,6 +180,16 @@ class InputTest < ActionView::TestCase
assert_select 'select option[selected=selected]', '18'
end
test 'input should automatically set include blank' do
with_input_for :age, :select, :collection => 18..30
assert_select 'select option[value=]', ""
end
test 'input should not set include blank if otherwise is told' do
with_input_for :age, :select, :collection => 18..30, :options => { :include_blank => false }
assert_no_select 'select option[value=]', ""
end
test 'input should allow overriding collection for radio types' do
with_input_for :name, :radio, :collection => ['Jose', 'Carlos']
assert_select 'input[type=radio][value=Jose]'