Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AquaGeek/969914 to your computer and use it in GitHub Desktop.
Save AquaGeek/969914 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #1432
From 062c30d12587015c6373dd78336d7a43d32a086e Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Jakub=20Ku=C5=BAma?= <[email protected]>
Date: Sat, 22 Nov 2008 14:04:12 +0100
Subject: [PATCH] check_box_tag should behave like radio_button_tag
---
.../lib/action_view/helpers/form_tag_helper.rb | 20 +++++++++-----------
actionpack/test/template/form_tag_helper_test.rb | 8 ++++----
2 files changed, 13 insertions(+), 15 deletions(-)
diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb
index 4646bc1..c133f54 100644
--- a/actionpack/lib/action_view/helpers/form_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -117,7 +117,7 @@ module ActionView
# Creates a label field
#
- # ==== Options
+ # ==== Options
# * Creates standard HTML attributes for the tag.
#
# ==== Examples
@@ -268,21 +268,21 @@ module ActionView
#
# ==== Examples
# check_box_tag 'accept'
- # # => <input id="accept" name="accept" type="checkbox" value="1" />
+ # # => <input id="accept_1" name="accept" type="checkbox" value="1" />
#
# check_box_tag 'rock', 'rock music'
- # # => <input id="rock" name="rock" type="checkbox" value="rock music" />
+ # # => <input id="rock_rock_music" name="rock" type="checkbox" value="rock music" />
#
# check_box_tag 'receive_email', 'yes', true
- # # => <input checked="checked" id="receive_email" name="receive_email" type="checkbox" value="yes" />
+ # # => <input checked="checked" id="receive_email_yes" name="receive_email" type="checkbox" value="yes" />
#
# check_box_tag 'tos', 'yes', false, :class => 'accept_tos'
- # # => <input class="accept_tos" id="tos" name="tos" type="checkbox" value="yes" />
+ # # => <input class="accept_tos" id="tos_yes" name="tos" type="checkbox" value="yes" />
#
# check_box_tag 'eula', 'accepted', false, :disabled => true
- # # => <input disabled="disabled" id="eula" name="eula" type="checkbox" value="accepted" />
+ # # => <input disabled="disabled" id="eula_accepted" name="eula" type="checkbox" value="accepted" />
def check_box_tag(name, value = "1", checked = false, options = {})
- html_options = { "type" => "checkbox", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
+ html_options = { "type" => "checkbox", "name" => name, "id" => sanitize_to_id("#{name}_#{value}"), "value" => value }.update(options.stringify_keys)
html_options["checked"] = "checked" if checked
tag :input, html_options
end
@@ -307,9 +307,7 @@ module ActionView
# radio_button_tag 'color', "green", true, :class => "color_input"
# # => <input checked="checked" class="color_input" id="color_green" name="color" type="radio" value="green" />
def radio_button_tag(name, value, checked = false, options = {})
- pretty_tag_value = value.to_s.gsub(/\s/, "_").gsub(/(?!-)\W/, "").downcase
- pretty_name = name.to_s.gsub(/\[/, "_").gsub(/\]/, "")
- html_options = { "type" => "radio", "name" => name, "id" => "#{pretty_name}_#{pretty_tag_value}", "value" => value }.update(options.stringify_keys)
+ html_options = { "type" => "radio", "name" => name, "id" => sanitize_to_id("#{name}_#{value}"), "value" => value }.update(options.stringify_keys)
html_options["checked"] = "checked" if checked
tag :input, html_options
end
@@ -351,7 +349,7 @@ module ActionView
if disable_with = options.delete("disable_with")
disable_with = "this.value='#{disable_with}'"
disable_with << ";#{options.delete('onclick')}" if options['onclick']
-
+
options["onclick"] = "if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }"
options["onclick"] << "else { hiddenCommit = this.cloneNode(false);hiddenCommit.setAttribute('type', 'hidden');this.form.appendChild(hiddenCommit); }"
options["onclick"] << "this.setAttribute('originalValue', this.value);this.disabled = true;#{disable_with};"
diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb
index 0c8af60..a98803a 100644
--- a/actionpack/test/template/form_tag_helper_test.rb
+++ b/actionpack/test/template/form_tag_helper_test.rb
@@ -16,7 +16,7 @@ class FormTagHelperTest < ActionView::TestCase
def test_check_box_tag
actual = check_box_tag "admin"
- expected = %(<input id="admin" name="admin" type="checkbox" value="1" />)
+ expected = %(<input id="admin_1" name="admin" type="checkbox" value="1" />)
assert_dom_equal expected, actual
end
@@ -236,8 +236,8 @@ class FormTagHelperTest < ActionView::TestCase
end
def test_boolean_options
- assert_dom_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_dom_equal %(<input checked="checked" id="admin" name="admin" type="checkbox" value="1" />), check_box_tag("admin", 1, true, :disabled => false, :readonly => nil)
+ assert_dom_equal %(<input checked="checked" disabled="disabled" id="admin_1" name="admin" readonly="readonly" type="checkbox" value="1" />), check_box_tag("admin", 1, true, 'disabled' => true, :readonly => "yes")
+ assert_dom_equal %(<input checked="checked" id="admin_1" name="admin" type="checkbox" value="1" />), check_box_tag("admin", 1, true, :disabled => false, :readonly => nil)
assert_dom_equal %(<input type="checkbox" />), tag(:input, :type => "checkbox", :checked => false)
assert_dom_equal %(<select id="people" multiple="multiple" name="people[]"><option>david</option></select>), select_tag("people", "<option>david</option>", :multiple => true)
assert_dom_equal %(<select id="people_" multiple="multiple" name="people[]"><option>david</option></select>), select_tag("people[]", "<option>david</option>", :multiple => true)
@@ -270,7 +270,7 @@ class FormTagHelperTest < ActionView::TestCase
submit_tag("Save", :confirm => "Are you sure?")
)
end
-
+
def test_image_submit_tag_with_confirmation
assert_dom_equal(
%(<input type="image" src="/images/save.gif" onclick="return confirm('Are you sure?');"/>),
--
1.5.6.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment