Skip to content

Commit

Permalink
I18n label helper [#745 status:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
Carsten Gehling authored and josevalim committed Jan 2, 2010
1 parent 653fa4c commit bef968d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
39 changes: 36 additions & 3 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -504,15 +504,39 @@ def fields_for(record_or_name_or_array, *args, &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
# assigned to the template (identified by +object+). The text of label will default to the attribute name unless a translation
# is found in the current I18n locale (through views.labels.<modelname>.<attribute>) or 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, 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)
# # => <label for="post_title">Title</label>
#
# You can localize your labels based on model and attribute names.
# For example you can define the following in your locale (e.g. en.yml)
#
# views:
# labels:
# post:
# body: "Write your entire text here"
#
# Which then will result in
#
# label(:post, :body)
# # => <label for="post_body">Write your entire text here</label>
#
# Localization can also be based purely on the translation of the attribute-name like this:
#
# activemodel:
# attribute:
# post:
# cost: "Total cost"
#
# label(:post, :cost)
# # => <label for="post_cost">Total cost</label>
#
# label(:post, :title, "A short title")
# # => <label for="post_title">A short title</label>
#
Expand Down Expand Up @@ -751,7 +775,16 @@ def to_label_tag(text = nil, options = {})
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

content = if text.blank?
i18n_label = I18n.t("views.labels.#{object_name}.#{method_name}", :default => "")
i18n_label if i18n_label.present?
else
text.to_s
end
content ||= object.class.human_attribute_name(method_name) if object
content ||= method_name.humanize

label_tag(name_and_id["id"], content, options)
end

Expand Down
1 change: 1 addition & 0 deletions actionpack/test/lib/controller/fake_models.rb
Expand Up @@ -54,6 +54,7 @@ class Store < Question
class Post < Struct.new(:title, :author_name, :body, :secret, :written_on, :cost)
extend ActiveModel::Naming
include ActiveModel::Conversion
extend ActiveModel::Translation

alias_method :secret?, :secret

Expand Down
40 changes: 40 additions & 0 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -6,6 +6,25 @@ class FormHelperTest < ActionView::TestCase

def setup
super

# Create "label" locale for testing I18n label helpers
I18n.backend.store_translations 'label', {
:activemodel => {
:attributes => {
:post => {
:cost => "Total cost"
}
}
},
:views => {
:labels => {
:post => {
:body => "Write entire text here"
}
}
}
}

@post = Post.new
@comment = Comment.new
def @post.errors()
Expand Down Expand Up @@ -51,6 +70,27 @@ def test_label_with_symbols
assert_dom_equal('<label for="post_secret">Secret?</label>', label(:post, :secret?))
end

def test_label_with_locales_strings
old_locale, I18n.locale = I18n.locale, :label
assert_dom_equal('<label for="post_body">Write entire text here</label>', label("post", "body"))
ensure
I18n.locale = old_locale
end

def test_label_with_human_attribute_name
old_locale, I18n.locale = I18n.locale, :label
assert_dom_equal('<label for="post_cost">Total cost</label>', label(:post, :cost))
ensure
I18n.locale = old_locale
end

def test_label_with_locales_symbols
old_locale, I18n.locale = I18n.locale, :label
assert_dom_equal('<label for="post_body">Write entire text here</label>', label(:post, :body))
ensure
I18n.locale = old_locale
end

def test_label_with_for_attribute_as_symbol
assert_dom_equal('<label for="my_for">Title</label>', label(:post, :title, nil, :for => "my_for"))
end
Expand Down

1 comment on commit bef968d

@yaroslav
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jose Can I has 2-3-stable please?

Please sign in to comment.