From d194d4cb9f66d48bbe96f11feba2f50c4e4b8494 Mon Sep 17 00:00:00 2001 From: David Stevenson Date: Mon, 16 Feb 2009 11:56:50 -0800 Subject: [PATCH] Allowing the "label" form helper to take a :value option which allows it to target the automatically generated IDs for radio_buttons. --- actionpack/lib/action_view/helpers/form_helper.rb | 27 +++++++++++++++----- actionpack/test/template/form_helper_test.rb | 7 +++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 568687e..3c6a8dc 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -464,7 +464,8 @@ module ActionView # 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 :value 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) @@ -476,6 +477,9 @@ module ActionView # label(:post, :title, "A short title", :class => "title_label") # # => # + # label(:post, :privacy, "Public Post", :value => "public") + # # => + # def label(object_name, method, text = nil, options = {}) InstanceTag.new(object_name, method, self, options.delete(:object)).to_label_tag(text, options) end @@ -689,8 +693,9 @@ module ActionView 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 @@ -722,11 +727,7 @@ module ActionView 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 @@ -827,6 +828,18 @@ module ActionView 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 + 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) + 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"]) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 5cc81b4..d8b3ba6 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -85,6 +85,13 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal('', label("post", "secret?")) end + def test_label_for_radio_buttons_with_value + assert_dom_equal('', + label("post", "title", "The title goes here", :value => "great_title")) + assert_dom_equal('', + label("post", "title", "The title goes here", :value => "great title")) + end + def test_label_with_symbols assert_dom_equal('', label(:post, :title)) assert_dom_equal('', label(:post, :secret?)) -- 1.6.0.1