Skip to content

Commit

Permalink
Do not override attribute value selection in collection select. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosantoniodasilva committed Sep 15, 2011
1 parent 6250781 commit 7aab179
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions lib/simple_form/action_view_extensions/builder.rb
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions test/inputs_test.rb
Expand Up @@ -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]'
Expand Down

0 comments on commit 7aab179

Please sign in to comment.