Skip to content

Commit cdb5108

Browse files
Merge pull request rails#45527 from ghiculescu/checked-kwarg
Support `checked` as a keyword argument in `check_box_tag` and `radio_button_tag`
2 parents 47070d8 + 4fcac15 commit cdb5108

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

actionview/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
* `check_box_tag` and `radio_button_tag` now accept `checked` as a keyword argument
2+
3+
This is to make the API more consistent with the `FormHelper` variants. You can now provide `checked` as a positional or keyword argument:
4+
5+
```erb
6+
= check_box_tag "admin", "1", false
7+
= check_box_tag "admin", "1", checked: false
8+
9+
= radio_button_tag 'favorite_color', 'maroon', false
10+
= radio_button_tag 'favorite_color', 'maroon', checked: false
11+
```
12+
13+
*Alex Ghiculescu*
14+
115
* Allow passing a class to `dom_id`.
216
You no longer need to call `new` when passing a class to `dom_id`.
317
This makes `dom_id` behave like `dom_class` in this regard.

actionview/lib/action_view/helpers/form_tag_helper.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,17 @@ def text_area_tag(name, content = nil, options = {})
422422
content_tag :textarea, content.to_s.html_safe, { "name" => name, "id" => sanitize_to_id(name) }.update(options)
423423
end
424424

425+
##
426+
# :call-seq:
427+
# check_box_tag(name, options = {})
428+
# check_box_tag(name, value, options = {})
429+
# check_box_tag(name, value, checked, options = {})
430+
#
425431
# Creates a check box form input tag.
426432
#
427433
# ==== Options
434+
# * <tt>:value</tt> - The value of the input. Defaults to <tt>"1"</tt>.
435+
# * <tt>:checked</tt> - If set to true, the checkbox will be checked by default.
428436
# * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
429437
# * Any other key creates standard HTML options for the tag.
430438
#
@@ -443,16 +451,27 @@ def text_area_tag(name, content = nil, options = {})
443451
#
444452
# check_box_tag 'eula', 'accepted', false, disabled: true
445453
# # => <input disabled="disabled" id="eula" name="eula" type="checkbox" value="accepted" />
446-
def check_box_tag(name, value = "1", checked = false, options = {})
454+
def check_box_tag(name, *args)
455+
if args.length >= 4
456+
raise ArgumentError, "wrong number of arguments (given #{args.length + 1}, expected 1..4)"
457+
end
458+
options = args.extract_options!
459+
value, checked = args.empty? ? ["1", false] : [*args, false]
447460
html_options = { "type" => "checkbox", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
448461
html_options["checked"] = "checked" if checked
449462
tag :input, html_options
450463
end
451464

465+
##
466+
# :call-seq:
467+
# radio_button_tag(name, value, options = {})
468+
# radio_button_tag(name, value, checked, options = {})
469+
#
452470
# Creates a radio button; use groups of radio buttons named the same to allow users to
453471
# select from a group of options.
454472
#
455473
# ==== Options
474+
# * <tt>:checked</tt> - If set to true, the radio button will be selected by default.
456475
# * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
457476
# * Any other key creates standard HTML options for the tag.
458477
#
@@ -468,7 +487,12 @@ def check_box_tag(name, value = "1", checked = false, options = {})
468487
#
469488
# radio_button_tag 'color', "green", true, class: "color_input"
470489
# # => <input checked="checked" class="color_input" id="color_green" name="color" type="radio" value="green" />
471-
def radio_button_tag(name, value, checked = false, options = {})
490+
def radio_button_tag(name, value, *args)
491+
if args.length >= 3
492+
raise ArgumentError, "wrong number of arguments (given #{args.length + 2}, expected 2..4)"
493+
end
494+
options = args.extract_options!
495+
checked = args.empty? ? false : args.first
472496
html_options = { "type" => "radio", "name" => name, "id" => "#{sanitize_to_id(name)}_#{sanitize_to_id(value)}", "value" => value }.update(options.stringify_keys)
473497
html_options["checked"] = "checked" if checked
474498
tag :input, html_options

actionview/test/template/form_tag_helper_test.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,36 @@ def test_check_box_tag_default_checked
8888
assert_dom_equal expected, actual
8989
end
9090

91+
def test_check_box_tag_checked_kwarg_true
92+
actual = check_box_tag "admin", "yes", checked: true
93+
expected = %(<input id="admin" checked="checked" name="admin" type="checkbox" value="yes" />)
94+
assert_dom_equal expected, actual
95+
end
96+
97+
def test_check_box_tag_checked_kwarg_false
98+
actual = check_box_tag "admin", "1", checked: false
99+
expected = %(<input id="admin" name="admin" type="checkbox" value="1" />)
100+
assert_dom_equal expected, actual
101+
end
102+
103+
def test_check_box_tag_checked_kwarg_false_and_disabled
104+
actual = check_box_tag "admin", "1", checked: false, disabled: true
105+
expected = %(<input id="admin" name="admin" type="checkbox" value="1" disabled="disabled" />)
106+
assert_dom_equal expected, actual
107+
end
108+
109+
def test_check_box_tag_checked_kwarg_true_value_argument_skipped
110+
actual = check_box_tag "admin", checked: true
111+
expected = %(<input id="admin" checked="checked" name="admin" type="checkbox" value="1" />)
112+
assert_dom_equal expected, actual
113+
end
114+
115+
def test_check_box_tag_value_kwarg
116+
actual = check_box_tag "admin", value: "0", checked: true
117+
expected = %(<input id="admin" name="admin" type="checkbox" value="0" checked="checked" />)
118+
assert_dom_equal expected, actual
119+
end
120+
91121
def test_check_box_tag_id_sanitized
92122
label_elem = root_elem(check_box_tag("project[2][admin]"))
93123
assert_match VALID_HTML_ID, label_elem["id"]
@@ -374,6 +404,30 @@ def test_radio_button_tag
374404
actual = radio_button_tag("ctrlname", "apache2.2")
375405
expected = %(<input id="ctrlname_apache2.2" name="ctrlname" type="radio" value="apache2.2" />)
376406
assert_dom_equal expected, actual
407+
408+
actual = radio_button_tag "people", "david", true
409+
expected = %(<input id="people_david" name="people" type="radio" value="david" checked="checked" />)
410+
assert_dom_equal expected, actual
411+
412+
actual = radio_button_tag "people", "david", false
413+
expected = %(<input id="people_david" name="people" type="radio" value="david" />)
414+
assert_dom_equal expected, actual
415+
416+
actual = radio_button_tag "people", "david", false, disabled: true
417+
expected = %(<input id="people_david" name="people" type="radio" value="david" disabled="disabled" />)
418+
assert_dom_equal expected, actual
419+
420+
actual = radio_button_tag "people", "david", checked: true
421+
expected = %(<input id="people_david" name="people" type="radio" value="david" checked="checked" />)
422+
assert_dom_equal expected, actual
423+
424+
actual = radio_button_tag "people", "david", checked: false
425+
expected = %(<input id="people_david" name="people" type="radio" value="david" />)
426+
assert_dom_equal expected, actual
427+
428+
actual = radio_button_tag "people", "david", checked: false, disabled: true
429+
expected = %(<input id="people_david" name="people" type="radio" value="david" disabled="disabled" />)
430+
assert_dom_equal expected, actual
377431
end
378432

379433
def test_select_tag

0 commit comments

Comments
 (0)