Permalink
Browse files

Do not override attribute value selection in collection select. Closes

…#325

Conflicts:

	test/inputs_test.rb
1 parent 183e687 commit 42307c1f3f79ae83b34dd73617b22b10e3e0b748 @carlosantoniodasilva carlosantoniodasilva committed Sep 15, 2011
Showing with 15 additions and 2 deletions.
  1. +1 −0 CHANGELOG.rdoc
  2. +5 −2 lib/simple_form/action_view_extensions/builder.rb
  3. +9 −0 test/inputs/collection_input_test.rb
View
@@ -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
@@ -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
@@ -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

0 comments on commit 42307c1

Please sign in to comment.