aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/form_helper.rb
diff options
context:
space:
mode:
authorDavid Stevenson <david@flouri.sh>2009-03-11 09:28:56 -0700
committerMichael Koziarski <michael@koziarski.com>2009-06-09 20:28:43 +1200
commita14df8c9b26b489f1db8fba64c72da3f20af7fcd (patch)
treebcbd3c15e4185c556b576594efaa34798005acbe /actionpack/lib/action_view/helpers/form_helper.rb
parent63091cef2eeed500c1cf930f04e653b269abe12b (diff)
downloadrails-a14df8c9b26b489f1db8fba64c72da3f20af7fcd.tar.gz
rails-a14df8c9b26b489f1db8fba64c72da3f20af7fcd.tar.bz2
rails-a14df8c9b26b489f1db8fba64c72da3f20af7fcd.zip
Made label target radio button tags with values. Radio button now respects inherited :index options when generating id.
Signed-off-by: Michael Koziarski <michael@koziarski.com>
Diffstat (limited to 'actionpack/lib/action_view/helpers/form_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb26
1 files changed, 19 insertions, 7 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 6668482370..8ecec87b10 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -495,7 +495,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 <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)
@@ -507,6 +508,9 @@ module ActionView
# 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
@@ -729,8 +733,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
@@ -762,11 +767,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
@@ -867,6 +868,17 @@ 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
+ 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"])