Skip to content

Commit

Permalink
Alias field_set_tag helper to fieldset_tag
Browse files Browse the repository at this point in the history
The [field_set_tag][] renders a `<fieldset>` element. At times, the
desire to render a `fieldset` results in calling `fieldset_tag`, only to
be surprised by a `NoMethodError`.

Originally, the method's name was `fieldset_tag` helper (defined in
[0e6c8e5][] in 2007), but was renamed in [73c7083][] (3 days later).

This commit aliases `field_set_tag` to `fieldset_tag` so that both are
available.

Additionally, defines the method so that it utilizes the [content_tag][]
so that it isn't responsible for manually managing the closing
(`</fieldset>`) of the opening `<fieldset>` tag.

[field_set_tag]: https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-field_set_tag
[0e6c8e5]: 0e6c8e5
[73c7083]: 73c7083
[content_tag]: https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag
  • Loading branch information
seanpdoyle committed Dec 2, 2023
1 parent b3b230c commit c5ae44d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions actionview/CHANGELOG.md
@@ -1,3 +1,7 @@
* Alias `field_set_tag` helper to `fieldset_tag` to match `<fieldset>` element

*Sean Doyle*

* Deprecate passing content to void elements when using `tag.br` type tag builders.

*Hartley McGuire*
Expand Down
10 changes: 6 additions & 4 deletions actionview/lib/action_view/helpers/form_tag_helper.rb
Expand Up @@ -675,11 +675,13 @@ def image_submit_tag(source, options = {})
# <% end %>
# # => <fieldset class="format"><p><input id="name" name="name" type="text" /></p></fieldset>
def field_set_tag(legend = nil, options = nil, &block)
output = tag(:fieldset, options, true)
output.safe_concat(content_tag("legend", legend)) unless legend.blank?
output.concat(capture(&block)) if block_given?
output.safe_concat("</fieldset>")
content = []
content << content_tag("legend", legend) unless legend.blank?
content << capture(&block) if block_given?

content_tag(:fieldset, safe_join(content), options)
end
alias_method :fieldset_tag, :field_set_tag

# Creates a text field of type "color".
#
Expand Down
5 changes: 5 additions & 0 deletions actionview/test/template/form_tag_helper_test.rb
Expand Up @@ -925,6 +925,11 @@ def test_field_set_tag_in_erb
expected = %(<fieldset class="format">Hello world!</fieldset>)
assert_dom_equal expected, output_buffer

output_buffer = render_erb("<%= fieldset_tag('', :class => 'format') do %>Hello world!<% end %>")

expected = %(<fieldset class="format">Hello world!</fieldset>)
assert_dom_equal expected, output_buffer

output_buffer = render_erb("<%= field_set_tag %>")

expected = %(<fieldset></fieldset>)
Expand Down

0 comments on commit c5ae44d

Please sign in to comment.