Skip to content

Commit

Permalink
[padrino-helpers] Fixes options_for_select helper selected logic and …
Browse files Browse the repository at this point in the history
…added test
  • Loading branch information
nesquena committed Jul 12, 2010
1 parent 44967af commit 80d499b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion padrino-helpers/lib/padrino-helpers/form_helpers.rb
Expand Up @@ -357,7 +357,7 @@ def options_for_select(option_items, selected_value=nil)
return '' if option_items.blank?
option_items.collect do |caption, value|
value ||= caption
content_tag(:option, caption, :value => value, :selected => selected_value.to_s =~ /#{value}|#{caption}/)
content_tag(:option, caption, :value => value, :selected => selected_value.to_s =~ /^(#{value}|#{caption})$/)
end
end

Expand Down
8 changes: 8 additions & 0 deletions padrino-helpers/test/test_form_helpers.rb
Expand Up @@ -351,6 +351,14 @@ def app
assert_has_tag('select option', :content => 'Blue', :value => 'blue1') { actual_html }
assert_has_tag('select option', :content => 'Black', :value => 'black1') { actual_html }
end

should "display options selected only for exact match" do
options = [['One', '1'], ['1', '10'], ['Two', "-1"]]
actual_html = select_tag(:range, :options => options, :selected => '-1')
assert_has_tag(:select, :name => 'range') { actual_html }
assert_has_tag('select option', :selected => 'selected', :count => 1) { actual_html }
assert_has_tag('select option', :content => 'Two', :value => '-1', :selected => 'selected') { actual_html }
end

should "display select tag in erb" do
visit '/erb/form_tag'
Expand Down

0 comments on commit 80d499b

Please sign in to comment.