Skip to content

Commit

Permalink
handle array of strings in :checked option
Browse files Browse the repository at this point in the history
  • Loading branch information
nashby committed May 6, 2012
1 parent 204fa86 commit 84ba5a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/simple_form/action_view_extensions/builder.rb
Expand Up @@ -223,7 +223,7 @@ def default_html_options_for_collection(item, value, options, html_options) #:no
accept = if current_option.respond_to?(:call)
current_option.call(item)
else
Array(current_option).include?(value)
Array(current_option).map(&:to_s).include?(value.to_s)
end

if accept
Expand Down
12 changes: 10 additions & 2 deletions test/action_view_extensions/builder_test.rb
Expand Up @@ -63,7 +63,7 @@ def with_collection_check_boxes(object, attribute, collection, value_method, tex
assert_select 'form input[type=radio][value=true][checked=checked]'
assert_no_select 'form input[type=radio][value=false][checked=checked]'
end

test 'collection radio accepts checked item which has a value of false' do
with_collection_radio_buttons @user, :active, [[1, true], [0, false]], :last, :first, :checked => false
assert_no_select 'form input[type=radio][value=true][checked=checked]'
Expand Down Expand Up @@ -302,7 +302,6 @@ def with_collection_check_boxes(object, attribute, collection, value_method, tex
assert_no_select 'form input[type=checkbox][value=2][checked=checked]'
end


test 'collection check box accepts selected values as :checked option' do
collection = (1..3).map{|i| [i, "Tag #{i}"] }
with_collection_check_boxes @user, :tag_ids, collection, :first, :last, :checked => [1, 3]
Expand All @@ -312,6 +311,15 @@ def with_collection_check_boxes(object, attribute, collection, value_method, tex
assert_no_select 'form input[type=checkbox][value=2][checked=checked]'
end

test 'collection check boxes accepts selected string values as :checked option' do
collection = (1..3).map{|i| [i, "Category #{i}"] }
with_collection_check_boxes :user, :category_ids, collection, :first, :last, :checked => ['1', '3']

assert_select 'input[type=checkbox][value=1][checked=checked]'
assert_select 'input[type=checkbox][value=3][checked=checked]'
assert_no_select 'input[type=checkbox][value=2][checked=checked]'
end

test 'collection check box accepts a single checked value' do
collection = (1..3).map{|i| [i, "Tag #{i}"] }
with_collection_check_boxes @user, :tag_ids, collection, :first, :last, :checked => 3
Expand Down

0 comments on commit 84ba5a3

Please sign in to comment.