Skip to content
This repository
Browse code

Fix human attribute_name to handle deeply nested attributes

This is a back-port of b0e2fc8 to Rails 3.2.
See #5843 and #3859 also.
  • Loading branch information...
commit 029936efbe7318e9146c22c6609e1571c7abb0c1 1 parent 3cba6ee
Tsutomu Kuroda authored June 06, 2012
4  activemodel/lib/active_model/translation.rb
@@ -44,9 +44,9 @@ def lookup_ancestors
44 44
     # Specify +options+ with additional translating options.
45 45
     def human_attribute_name(attribute, options = {})
46 46
       defaults  = []
47  
-      parts     = attribute.to_s.split(".", 2)
  47
+      parts     = attribute.to_s.split(".")
48 48
       attribute = parts.pop
49  
-      namespace = parts.pop
  49
+      namespace = parts.join("/") unless parts.empty?
50 50
 
51 51
       if namespace
52 52
         lookup_ancestors.each do |klass|
5  activemodel/test/cases/translation_test.rb
@@ -56,6 +56,11 @@ def test_translated_model_attributes_with_attribute_matching_namespaced_model_na
56 56
     assert_equal 'person gender attribute', Person::Gender.human_attribute_name('attribute')
57 57
   end
58 58
 
  59
+  def test_translated_deeply_nested_model_attributes
  60
+    I18n.backend.store_translations 'en', :activemodel => {:attributes => {:"person/contacts/addresses" => {:street => 'Deeply Nested Address Street'}}}
  61
+    assert_equal 'Deeply Nested Address Street', Person.human_attribute_name('contacts.addresses.street')
  62
+  end
  63
+
59 64
   def test_translated_nested_model_attributes
60 65
     I18n.backend.store_translations 'en', :activemodel => {:attributes => {:"person/addresses" => {:street => 'Person Address Street'}}}
61 66
     assert_equal 'Person Address Street', Person.human_attribute_name('addresses.street')

0 notes on commit 029936e

Please sign in to comment.
Something went wrong with that request. Please try again.