diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index f0668232..e6084623 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -6,6 +6,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 05a0ef4c..8706be02 100644 --- a/lib/simple_form/action_view_extensions/builder.rb +++ b/lib/simple_form/action_view_extensions/builder.rb @@ -178,8 +178,11 @@ class ActionView::Helpers::FormBuilder [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/collection_input_test.rb b/test/inputs/collection_input_test.rb index 2f24eaa5..986b2b2d 100644 --- a/test/inputs/collection_input_test.rb +++ b/test/inputs/collection_input_test.rb @@ -298,4 +298,13 @@ class CollectionInputTest < ActionView::TestCase assert_select 'select option[value=Antonio]', 'ANTONIO' 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 end