diff options
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_tag_helper.rb | 34 | ||||
-rw-r--r-- | actionpack/test/template/form_tag_helper_test.rb | 6 |
3 files changed, 34 insertions, 8 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 0a7435e799..3595b54a81 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added that the html options disabled, readonly, and multiple can all be treated as booleans. So specifying <tt>disabled => :true</tt> will give <tt>disabled="disabled"</tt>. #809 [mindel] + * Added path collection syntax for Routes that will gobble up the rest of the url and pass it on to the controller #830 [rayners]. Example: map.connect 'categories/*path_info', :controller => 'categories', :action => 'show' diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 2fc055686b..d8535d2870 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -5,6 +5,9 @@ module ActionView module Helpers # Provides a number of methods for creating form tags that doesn't rely on conventions with an object assigned to the template like # FormHelper does. With the FormTagHelper, you provide the names and values yourself. + # + # NOTE: The html options disabled, readonly, and multiple can all be treated as booleans. So specifying <tt>disabled => :true</tt> + # will give <tt>disabled="disabled"</tt>. module FormTagHelper # Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like # ActionController::Base#url_for. The method for the form defaults to POST. @@ -31,11 +34,11 @@ module ActionView end def select_tag(name, option_tags = nil, options = {}) - content_tag("select", option_tags, { "name" => name, "id" => name }.update(options.stringify_keys)) + content_tag("select", option_tags, { "name" => name, "id" => name }.update(convert_options(options))) end def text_field_tag(name, value = nil, options = {}) - tag("input", { "type" => "text", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys)) + tag("input", { "type" => "text", "name" => name, "id" => name, "value" => value }.update(convert_options(options))) end def hidden_field_tag(name, value = nil, options = {}) @@ -43,11 +46,11 @@ module ActionView end def file_field_tag(name, options = {}) - text_field_tag(name, nil, options.stringify_keys.update("type" => "file")) + text_field_tag(name, nil, convert_options(options).update("type" => "file")) end def password_field_tag(name = "password", value = nil, options = {}) - text_field_tag(name, value, options.stringify_keys.update("type" => "password")) + text_field_tag(name, value, convert_options(options).update("type" => "password")) end def text_area_tag(name, content = nil, options = {}) @@ -57,24 +60,39 @@ module ActionView options.delete("size") end - content_tag("textarea", content, { "name" => name, "id" => name }.update(options.stringify_keys)) + content_tag("textarea", content, { "name" => name, "id" => name }.update(convert_options(options))) end def check_box_tag(name, value = "1", checked = false, options = {}) - html_options = { "type" => "checkbox", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys) + html_options = { "type" => "checkbox", "name" => name, "id" => name, "value" => value }.update(convert_options(options)) html_options["checked"] = "checked" if checked tag("input", html_options) end def radio_button_tag(name, value, checked = false, options = {}) - html_options = { "type" => "radio", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys) + html_options = { "type" => "radio", "name" => name, "id" => name, "value" => value }.update(convert_options(options)) html_options["checked"] = "checked" if checked tag("input", html_options) end def submit_tag(value = "Save changes", options = {}) - tag("input", { "type" => "submit", "name" => "submit", "value" => value }.update(options.stringify_keys)) + tag("input", { "type" => "submit", "name" => "submit", "value" => value }.update(convert_options(options))) end + + private + def convert_options(options) + options = options.stringify_keys + %w( disabled readonly multiple ).each { |a| boolean_attribute(options, a) } + options + end + + def boolean_attribute(options, attribute) + if options[attribute] + options[attribute] = attribute + else + options.delete attribute + end + end end end end diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index ab597d61a0..7a666271c1 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -81,5 +81,11 @@ class FormTagHelperTest < Test::Unit::TestCase assert_equal expected, actual end + def test_boolean_optios + assert_equal %(<input checked="checked" disabled="disabled" id="admin" name="admin" readonly="readonly" type="checkbox" value="1" />), check_box_tag("admin", 1, true, 'disabled' => true, :readonly => "yes") + assert_equal %(<input checked="checked" id="admin" name="admin" type="checkbox" value="1" />), check_box_tag("admin", 1, true, :disabled => false, :readonly => nil) + assert_equal %(<select id="people" multiple="multiple" name="people"><option>david</option></select>), select_tag("people", "<option>david</option>", :multiple => true) + assert_equal %(<select id="people" name="people"><option>david</option></select>), select_tag("people", "<option>david</option>", :multiple => nil) + end end |