Added default form values for select menus.

This commit is contained in:
Luke Melia 2008-03-12 02:04:41 -04:00
parent f7420463fd
commit 9fe0ef33af
3 changed files with 61 additions and 14 deletions

View File

@ -377,30 +377,25 @@ module ActionController
add_default_params_from_checkboxes_for(form) add_default_params_from_checkboxes_for(form)
add_default_params_from_radio_buttons_for(form) add_default_params_from_radio_buttons_for(form)
add_default_params_from_textareas_for(form) add_default_params_from_textareas_for(form)
add_default_params_from_selects_for(form)
end end
def add_default_params_from_inputs_for(form) # :nodoc: def add_default_params_from_inputs_for(form) # :nodoc:
(form / "input").each do |input| ((form / "input[@type='text']") + (form / "input[@type='hidden']")).each do |input|
next if input.attributes["value"].blank? || !%w[text hidden].include?(input.attributes["type"]) next if input.attributes["value"].blank?
add_form_data(input, input.attributes["value"]) add_form_data(input, input.attributes["value"])
end end
end end
def add_default_params_from_checkboxes_for(form) # :nodoc: def add_default_params_from_checkboxes_for(form) # :nodoc:
(form / "input").each do |input| (form / "input[@type='checkbox][@checked='checked']").each do |input|
next if input.attributes["type"] != "checkbox" add_form_data(input, input.attributes["value"] || "on")
if input.attributes["checked"] == "checked"
add_form_data(input, input.attributes["value"] || "on")
end
end end
end end
def add_default_params_from_radio_buttons_for(form) # :nodoc: def add_default_params_from_radio_buttons_for(form) # :nodoc:
(form / "input").each do |input| (form / "input[@type='radio][@checked='checked']").each do |input|
next if input.attributes["type"] != "radio" add_form_data(input, input.attributes["value"])
if input.attributes["checked"] == "checked"
add_form_data(input, input.attributes["value"])
end
end end
end end
@ -410,6 +405,16 @@ module ActionController
end end
end end
def add_default_params_from_selects_for(form) # :nodoc:
(form / "select").each do |select|
selected_options = select / "option[@selected='selected']"
selected_options = select / "option:first" if selected_options.empty?
selected_options.each do |option|
add_form_data(select, option.attributes["value"] || option.innerHTML)
end
end
end
def form_for_node(node) # :nodoc: def form_for_node(node) # :nodoc:
return node if node.name == "form" return node if node.name == "form"
node = node.parent until node.name == "form" node = node.parent until node.name == "form"

View File

@ -219,6 +219,48 @@ class ClicksButtonTest < Test::Unit::TestCase
@session.clicks_button @session.clicks_button
end end
def test_should_send_default_selected_option_value_from_select
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<select name="month">
<option value="1">January</option>
<option value="2" selected="selected">February</option>
</select>
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "month" => "2")
@session.clicks_button
end
def test_should_send_default_selected_option_inner_html_from_select_when_no_value_attribute
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<select name="month">
<option>January</option>
<option selected="selected">February</option>
</select>
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "month" => "February")
@session.clicks_button
end
def test_should_send_first_select_option_value_when_no_option_selected
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
</select>
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "month" => "1")
@session.clicks_button
end
def test_should_handle_nested_properties def test_should_handle_nested_properties
@response.stubs(:body).returns(<<-EOS) @response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login"> <form method="post" action="/login">

View File

@ -59,7 +59,7 @@ class SelectsTest < Test::Unit::TestCase
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "end_month" => "e1") @session.expects(:post_via_redirect).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects "January", :from => "end_month" @session.selects "January", :from => "end_month"
@session.clicks_button @session.clicks_button
end end
@ -74,7 +74,7 @@ class SelectsTest < Test::Unit::TestCase
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "end_month" => "e1") @session.expects(:post_via_redirect).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects "January", :from => "End Month" @session.selects "January", :from => "End Month"
@session.clicks_button @session.clicks_button
end end