1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added FormHelper#label (closes #8641) [jcoglan]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7541 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2007-09-22 17:17:22 +00:00
parent a5af3f75af
commit a7764d8fd4
3 changed files with 49 additions and 1 deletions

View file

@ -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]

View file

@ -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

View file

@ -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' />" +