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

Handle disabled and selected procs options for collection select. Closes #307

When giving label_method or value_method as procs, we were handling them
manually inside an overriden part of collection_select helper, but both disabled
and selected options were not being handled, causing errors when Rails attempted
to use such options.
This commit is contained in:
Carlos Antonio da Silva 2011-09-03 12:38:52 -03:00
parent 7a15d3d47e
commit 59c7e68009
2 changed files with 49 additions and 2 deletions

View file

@ -114,7 +114,7 @@ module SimpleForm
def default_html_options_for_collection(item, value, options, html_options) #:nodoc:
html_options = html_options.dup
[:checked, :disabled].each do |option|
[:checked, :selected, :disabled].each do |option|
next unless options[option]
accept = if options[option].is_a?(Proc)
@ -171,8 +171,15 @@ class ActionView::Helpers::FormBuilder
collection = collection.map do |item|
value = value_for_collection(item, value_method)
text = value_for_collection(item, text_method)
[value, text]
default_html_options = default_html_options_for_collection(item, value, options, html_options)
disabled = value if default_html_options[:disabled]
selected = value if default_html_options[:selected]
[value, text, selected, disabled]
end
options[:disabled] = collection.map(&:pop).compact
options[:selected] = collection.map(&:pop).compact
value_method, text_method = :first, :last
end

View file

@ -799,6 +799,46 @@ class InputTest < ActionView::TestCase
assert_no_select 'div.disabled'
end
test 'input should allow disabled options with a lambda for collection select' do
with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"],
:disabled => lambda { |x| x == "Carlos" }
assert_select 'select option[value=Carlos][disabled=disabled]', 'Carlos'
assert_select 'select option[value=Antonio]', 'Antonio'
assert_no_select 'select option[value=Antonio][disabled]'
end
test 'input should allow disabled and label method with lambdas for collection select' do
with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"],
:disabled => lambda { |x| x == "Carlos" }, :label_method => lambda { |x| x.upcase }
assert_select 'select option[value=Carlos][disabled=disabled]', 'CARLOS'
assert_select 'select option[value=Antonio]', 'ANTONIO'
assert_no_select 'select option[value=Antonio][disabled]'
end
test 'input should allow a non lambda disabled option with lambda label method for collections' do
with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"],
:disabled => "Carlos", :label_method => lambda { |x| x.upcase }
assert_select 'select option[value=Carlos][disabled=disabled]', 'CARLOS'
assert_select 'select option[value=Antonio]', 'ANTONIO'
assert_no_select 'select option[value=Antonio][disabled]'
end
test 'input should allow selected and label method with lambdas for collection select' do
with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"],
:selected => lambda { |x| x == "Carlos" }, :label_method => lambda { |x| x.upcase }
assert_select 'select option[value=Carlos][selected=selected]', 'CARLOS'
assert_select 'select option[value=Antonio]', 'ANTONIO'
assert_no_select 'select option[value=Antonio][selected]'
end
test 'input should allow a non lambda selected option with lambda label method for collection select' do
with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"],
:selected => "Carlos", :label_method => lambda { |x| x.upcase }
assert_select 'select option[value=Carlos][selected=selected]', 'CARLOS'
assert_select 'select option[value=Antonio]', 'ANTONIO'
assert_no_select 'select option[value=Antonio][selected]'
end
test 'input should allow overriding collection for radio types' do
with_input_for @user, :name, :radio, :collection => ['Jose', 'Carlos']
assert_select 'input[type=radio][value=Jose]'