diff --git a/lib/simple_form/action_view_extensions/builder.rb b/lib/simple_form/action_view_extensions/builder.rb index 612e6d1b..777341f0 100644 --- a/lib/simple_form/action_view_extensions/builder.rb +++ b/lib/simple_form/action_view_extensions/builder.rb @@ -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 diff --git a/test/inputs_test.rb b/test/inputs_test.rb index 823df4bc..f5ede621 100644 --- a/test/inputs_test.rb +++ b/test/inputs_test.rb @@ -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]'