Skip to content

Commit e2d3060

Browse files
committed
Merge pull request rails#3654 from wildchild/label_i18n
Fix impractical I18n lookup in nested fields_for
2 parents e6f3299 + c2b6f63 commit e2d3060

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

actionpack/lib/action_view/helpers/form_helper.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,8 +995,16 @@ def to_label_tag(text = nil, options = {}, &block)
995995
label_tag(name_and_id["id"], options, &block)
996996
else
997997
content = if text.blank?
998+
object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1')
998999
method_and_value = tag_value.present? ? "#{method_name}.#{tag_value}" : method_name
999-
I18n.t("helpers.label.#{object_name}.#{method_and_value}", :default => "").presence
1000+
1001+
if object.respond_to?(:to_model)
1002+
key = object.class.model_name.i18n_key
1003+
i18n_default = ["#{key}.#{method_and_value}".to_sym, ""]
1004+
end
1005+
1006+
i18n_default ||= ""
1007+
I18n.t("#{object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.label").presence
10001008
else
10011009
text.to_s
10021010
end

actionpack/test/template/form_helper_test.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ def setup
2727
:body => "Write entire text here",
2828
:color => {
2929
:red => "Rojo"
30+
},
31+
:comments => {
32+
:body => "Write body here"
3033
}
34+
},
35+
:tag => {
36+
:value => "Tag"
3137
}
3238
}
3339
}
@@ -68,6 +74,12 @@ def @post.to_param; '123'; end
6874
@post.secret = 1
6975
@post.written_on = Date.new(2004, 6, 15)
7076

77+
@post.comments = []
78+
@post.comments << @comment
79+
80+
@post.tags = []
81+
@post.tags << Tag.new
82+
7183
@blog_post = Blog::Post.new("And his name will be forty and four.", 44)
7284
end
7385

@@ -151,6 +163,40 @@ def test_label_with_locales_and_value
151163
I18n.locale = old_locale
152164
end
153165

166+
def test_label_with_locales_and_nested_attributes
167+
old_locale, I18n.locale = I18n.locale, :label
168+
form_for(@post, :html => { :id => 'create-post' }) do |f|
169+
f.fields_for(:comments) do |cf|
170+
concat cf.label(:body)
171+
end
172+
end
173+
174+
expected = whole_form("/posts/123", "create-post" , "edit_post", :method => "put") do
175+
"<label for=\"post_comments_attributes_0_body\">Write body here</label>"
176+
end
177+
178+
assert_dom_equal expected, output_buffer
179+
ensure
180+
I18n.locale = old_locale
181+
end
182+
183+
def test_label_with_locales_fallback_and_nested_attributes
184+
old_locale, I18n.locale = I18n.locale, :label
185+
form_for(@post, :html => { :id => 'create-post' }) do |f|
186+
f.fields_for(:tags) do |cf|
187+
concat cf.label(:value)
188+
end
189+
end
190+
191+
expected = whole_form("/posts/123", "create-post" , "edit_post", :method => "put") do
192+
"<label for=\"post_tags_attributes_0_value\">Tag</label>"
193+
end
194+
195+
assert_dom_equal expected, output_buffer
196+
ensure
197+
I18n.locale = old_locale
198+
end
199+
154200
def test_label_with_for_attribute_as_symbol
155201
assert_dom_equal('<label for="my_for">Title</label>', label(:post, :title, nil, :for => "my_for"))
156202
end

0 commit comments

Comments
 (0)