aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2008-01-21 21:08:28 +0000
committerMichael Koziarski <michael@koziarski.com>2008-01-21 21:08:28 +0000
commit7701e6466dd5f6ca1b3eb1bfaf73f1d0723bb324 (patch)
tree69e1c6e1c573135b3e460fc90d54ca70f662b219
parent37f71a77cc92df0960ef857880ccf9749a6eecc1 (diff)
downloadrails-7701e6466dd5f6ca1b3eb1bfaf73f1d0723bb324.tar.gz
rails-7701e6466dd5f6ca1b3eb1bfaf73f1d0723bb324.tar.bz2
rails-7701e6466dd5f6ca1b3eb1bfaf73f1d0723bb324.zip
Add label_tag helper for generating elements. Closes #10802 [DefV]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8685 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_view/helpers/form_tag_helper.rb18
-rw-r--r--actionpack/test/template/form_tag_helper_test.rb18
3 files changed, 38 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 1258967889..7f9a948f66 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add label_tag helper for generating elements. #10802 [DefV]
+
* Introduce TemplateFinder to handle view paths and lookups. #10800 [Pratik Naik]
* Performance: optimize route recognition. Large speedup for apps with many resource routes. #10835 [oleganza]
diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb
index 077465086b..a536b5bf67 100644
--- a/actionpack/lib/action_view/helpers/form_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -114,6 +114,24 @@ module ActionView
tag :input, { "type" => "text", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys)
end
+ # Creates a label field
+ #
+ # ==== Options
+ # * Creates standard HTML attributes for the tag.
+ #
+ # ==== Examples
+ # label_tag 'name'
+ # # => <label for="name">Name</label>
+ #
+ # label_tag 'name', 'Your name'
+ # # => <label for="name">Your Name</label>
+ #
+ # label_tag 'name', nil, :class => 'small_label'
+ # # => <label for="name" class="small_label">Name</label>
+ def label_tag(name, text = nil, options = {})
+ content_tag :label, text || name.humanize, { "for" => name }.update(options.stringify_keys)
+ end
+
# Creates a hidden form input field used to transmit data that would be lost due to HTTP's statelessness or
# data that should be hidden from the user.
#
diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb
index 3ca3e743eb..4160c838cc 100644
--- a/actionpack/test/template/form_tag_helper_test.rb
+++ b/actionpack/test/template/form_tag_helper_test.rb
@@ -188,6 +188,24 @@ class FormTagHelperTest < Test::Unit::TestCase
assert_dom_equal expected, actual
end
+ def test_label_tag_without_text
+ actual = label_tag "title"
+ expected = %(<label for="title">Title</label>)
+ assert_dom_equal expected, actual
+ end
+
+ def test_label_tag_with_text
+ actual = label_tag "title", "My Title"
+ expected = %(<label for="title">My Title</label>)
+ assert_dom_equal expected, actual
+ end
+
+ def test_label_tag_class_string
+ actual = label_tag "title", "My Title", "class" => "small_label"
+ expected = %(<label for="title" class="small_label">My Title</label>)
+ assert_dom_equal expected, actual
+ end
+
def test_boolean_optios
assert_dom_equal %(<input checked="checked" disabled="disabled" id="admin" name="admin" readonly="readonly" type="checkbox" value="1" />), check_box_tag("admin", 1, true, 'disabled' => true, :readonly => "yes")
assert_dom_equal %(<input checked="checked" id="admin" name="admin" type="checkbox" value="1" />), check_box_tag("admin", 1, true, :disabled => false, :readonly => nil)