From a7764d8fd491fb49f50397aa79d305bdb93552c5 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 22 Sep 2007 17:17:22 +0000 Subject: [PATCH] Added FormHelper#label (closes #8641) [jcoglan] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7541 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ .../lib/action_view/helpers/form_helper.rb | 33 ++++++++++++++++++- actionpack/test/template/form_helper_test.rb | 15 +++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) 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(:post, :title, "A short title") + # #=> + # + # label(:post, :title, "A short title", :class => "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("post", "title")) + assert_dom_equal('', label("post", "title", "The title goes here")) + assert_dom_equal( + '', + label("post", "title", nil, :class => 'title_label') + ) + end + + def test_label_with_symbols + assert_dom_equal('', label(:post, :title)) + end + def test_text_field assert_dom_equal( '', 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 = "
" + + "" + "" + "" + "" +