diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index c37b43699..d8190d926 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -5,6 +5,7 @@ * bug fix * Fallback to default label when block is provided (github.com/pivotal-casebook) + * Do not override default selection through attribute value in collection select when label/value methods are lambdas == 1.5.0 diff --git a/lib/simple_form/action_view_extensions/builder.rb b/lib/simple_form/action_view_extensions/builder.rb index 05a0ef4c3..8706be024 100644 --- a/lib/simple_form/action_view_extensions/builder.rb +++ b/lib/simple_form/action_view_extensions/builder.rb @@ -178,8 +178,11 @@ def collection_select(attribute, collection, value_method, text_method, options= [value, text, selected, disabled] end - options[:disabled] = collection.map(&:pop).compact - options[:selected] = collection.map(&:pop).compact + + [:disabled, :selected].each do |option| + option_value = collection.map(&:pop).compact + options[option] = option_value if option_value.present? + end value_method, text_method = :first, :last end diff --git a/test/inputs_test.rb b/test/inputs_test.rb index 34d54199f..d06e46bc0 100644 --- a/test/inputs_test.rb +++ b/test/inputs_test.rb @@ -847,6 +847,15 @@ def with_input_for(object, attribute_name, type, options={}) assert_no_select 'select option[value=Antonio][selected]' end + test 'input should not override default selection through attribute value with label method as lambda for collection select' do + @user.name = "Carlos" + with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"], + :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]'