aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb33
-rw-r--r--actionpack/test/template/form_helper_test.rb15
3 files changed, 49 insertions, 1 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 202ddba8ab..9a73ee76c1 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added FormHelper#label #8641 [jcoglan]
+
* Added AtomFeedHelper (slightly improved from the atom_feed_helper plugin) [DHH]
* Prevent errors when generating routes for uncountable resources, (i.e. sheep where plural == singluar). map.resources :sheep now creates sheep_index_url for the collection and sheep_url for the specific item. [Koz]
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 9c31fb6c90..9bfc3041af 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -219,6 +219,25 @@ module ActionView
yield builder.new(object_name, object, self, options, block)
end
+ # 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.
+ #
+ # ==== Examples
+ # label(:post, :title)
+ # #=> <label for="post_title">Title</label>
+ #
+ # label(:post, :title, "A short title")
+ # #=> <label for="post_title">A short title</label>
+ #
+ # label(:post, :title, "A short title", :class => "title_label")
+ # #=> <label for="post_title" class="title_label">A short title</label>
+ #
+ def label(object_name, method, text = nil, options = {})
+ InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_label_tag(text, options)
+ end
+
# Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). Additional options on the input 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
@@ -398,6 +417,14 @@ module ActionView
end
end
+ def to_label_tag(text = nil, options = {})
+ name_and_id = options.dup
+ add_default_name_and_id(name_and_id)
+ options["for"] = name_and_id["id"]
+ content = (text.blank? ? nil : text.to_s) || method_name.humanize
+ content_tag("label", content, options)
+ end
+
def to_input_field_tag(field_type, options = {})
options = options.stringify_keys
options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size")
@@ -574,7 +601,7 @@ module ActionView
@object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
end
- (field_helpers - %w(check_box radio_button fields_for)).each do |selector|
+ (field_helpers - %w(label check_box radio_button fields_for)).each do |selector|
src = <<-end_src
def #{selector}(method, options = {})
@template.send(#{selector.inspect}, @object_name, method, options.merge(:object => @object))
@@ -588,6 +615,10 @@ module ActionView
@template.fields_for(name, *args, &block)
end
+ def label(method, text = nil, options = {})
+ @template.label(@object_name, method, text, options.merge(:object => @object))
+ end
+
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
@template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value)
end
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index a852f2af17..af63a056f8 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -70,6 +70,19 @@ class FormHelperTest < Test::Unit::TestCase
@controller = @controller.new
end
+ def test_label
+ assert_dom_equal('<label for="post_title">Title</label>', label("post", "title"))
+ assert_dom_equal('<label for="post_title">The title goes here</label>', label("post", "title", "The title goes here"))
+ assert_dom_equal(
+ '<label class="title_label" for="post_title">Title</label>',
+ label("post", "title", nil, :class => 'title_label')
+ )
+ end
+
+ def test_label_with_symbols
+ assert_dom_equal('<label for="post_title">Title</label>', label(:post, :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")
@@ -275,6 +288,7 @@ class FormHelperTest < Test::Unit::TestCase
_erbout = ''
form_for(:post, @post, :html => { :id => 'create-post' }) do |f|
+ _erbout.concat f.label(:title)
_erbout.concat f.text_field(:title)
_erbout.concat f.text_area(:body)
_erbout.concat f.check_box(:secret)
@@ -283,6 +297,7 @@ class FormHelperTest < Test::Unit::TestCase
expected =
"<form action='http://www.example.com' id='create-post' method='post'>" +
+ "<label for='post_title'>Title</label>" +
"<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
"<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +