Skip to content

Commit

Permalink
Made label target radio button tags with values. Radio button now res…
Browse files Browse the repository at this point in the history
…pects inherited :index options when generating id.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information
dsboulder authored and NZKoz committed Jun 9, 2009
1 parent 63091ce commit a14df8c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
26 changes: 19 additions & 7 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -495,7 +495,8 @@ def fields_for(record_or_name_or_array, *args, &block)
# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). The text of label will default to the attribute name unless you specify
# it explicitly. Additional options on the label tag can be passed as a hash with +options+. These options will be tagged
# onto the HTML as an HTML element attribute as in the example shown.
# onto the HTML as an HTML element attribute as in the example shown, except for the <tt>:value</tt> option, which is designed to
# target labels for radio_button tags (where the value is used in the ID of the input tag).
#
# ==== Examples
# label(:post, :title)
Expand All @@ -507,6 +508,9 @@ def fields_for(record_or_name_or_array, *args, &block)
# label(:post, :title, "A short title", :class => "title_label")
# # => <label for="post_title" class="title_label">A short title</label>
#
# label(:post, :privacy, "Public Post", :value => "public")
# # => <label for="post_privacy_public">Public Post</label>
#
def label(object_name, method, text = nil, options = {})
InstanceTag.new(object_name, method, self, options.delete(:object)).to_label_tag(text, options)
end
Expand Down Expand Up @@ -729,8 +733,9 @@ def initialize(object_name, method_name, template_object, object = nil)

def to_label_tag(text = nil, options = {})
options = options.stringify_keys
tag_value = options.delete("value")
name_and_id = options.dup
add_default_name_and_id(name_and_id)
add_default_name_and_id_for_value(tag_value, name_and_id)
options.delete("index")
options["for"] ||= name_and_id["id"]
content = (text.blank? ? nil : text.to_s) || method_name.humanize
Expand Down Expand Up @@ -762,11 +767,7 @@ def to_radio_button_tag(tag_value, options = {})
checked = self.class.radio_button_checked?(value(object), tag_value)
end
options["checked"] = "checked" if checked
pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
options["id"] ||= defined?(@auto_index) ?
"#{tag_id_with_index(@auto_index)}_#{pretty_tag_value}" :
"#{tag_id}_#{pretty_tag_value}"
add_default_name_and_id(options)
add_default_name_and_id_for_value(tag_value, options)
tag("input", options)
end

Expand Down Expand Up @@ -867,6 +868,17 @@ def radio_button_checked?(value, checked_value)
end

private
def add_default_name_and_id_for_value(tag_value, options)
if tag_value
pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
specified_id = options["id"]
add_default_name_and_id(options)
options["id"] += "_#{pretty_tag_value}" unless specified_id
else
add_default_name_and_id(options)
end
end

def add_default_name_and_id(options)
if options.has_key?("index")
options["name"] ||= tag_name_with_index(options["index"])
Expand Down
19 changes: 19 additions & 0 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -99,6 +99,11 @@ def test_label_with_for_attribute_as_string
assert_dom_equal('<label for="my_for">Title</label>', label(:post, :title, nil, "for" => "my_for"))
end

def test_label_for_radio_buttons_with_value
assert_dom_equal('<label for="post_title_great_title">The title goes here</label>', label("post", "title", "The title goes here", :value => "great_title"))
assert_dom_equal('<label for="post_title_great_title">The title goes here</label>', label("post", "title", "The title goes here", :value => "great title"))
end

def test_text_field
assert_dom_equal(
'<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title")
Expand Down Expand Up @@ -532,6 +537,20 @@ def test_nested_fields_for_with_auto_index
assert_dom_equal expected, output_buffer
end

def test_nested_fields_for_with_index_radio_button
form_for(:post, @post) do |f|
f.fields_for(:comment, @post, :index => 5) do |c|
concat c.radio_button(:title, "hello")
end
end

expected = "<form action='http://www.example.com' method='post'>" +
"<input name='post[comment][5][title]' type='radio' id='post_comment_5_title_hello' value='hello' />" +
"</form>"

assert_dom_equal expected, output_buffer
end

def test_nested_fields_for_with_auto_index_on_both
form_for("post[]", @post) do |f|
f.fields_for("comment[]", @post) do |c|
Expand Down

0 comments on commit a14df8c

Please sign in to comment.