Skip to content
Browse files

Namespaced attribute lookup now works as 'model/association.attribute'.

  • Loading branch information...
1 parent 2985151 commit d4964b338667fb14d7755cd90af88bb267238958 @josevalim josevalim committed Dec 5, 2011
Showing with 19 additions and 11 deletions.
  1. +14 −6 activemodel/lib/active_model/translation.rb
  2. +5 −5 activemodel/test/cases/translation_test.rb
View
20 activemodel/lib/active_model/translation.rb
@@ -43,17 +43,25 @@ def lookup_ancestors
#
# Specify +options+ with additional translating options.
def human_attribute_name(attribute, options = {})
- defaults = []
- lookup_ancestors.each do |klass|
- if attribute.match(/\./)
- defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute.gsub(/\./, '/')}"
+ defaults = []
+ parts = attribute.to_s.split(".", 2)
+ attribute = parts.pop
+ namespace = parts.pop
+
+ if namespace
+ lookup_ancestors.each do |klass|
+ defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}"
+ end
+ defaults << :"#{self.i18n_scope}.attributes.#{namespace}.#{attribute}"
+ else
+ lookup_ancestors.each do |klass|
+ defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}"
end
- defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}"
end
defaults << :"attributes.#{attribute}"
defaults << options.delete(:default) if options[:default]
- defaults << attribute.to_s.humanize
+ defaults << attribute.humanize
options.reverse_merge! :count => 1, :default => defaults
I18n.translate(defaults.shift, options)
View
10 activemodel/test/cases/translation_test.rb
@@ -57,13 +57,13 @@ def test_translated_model_attributes_with_attribute_matching_namespaced_model_na
end
def test_translated_nested_model_attributes
- I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:"addresses/street" => 'Street'}}}
- assert_equal 'Street', Person.human_attribute_name('addresses.street')
+ I18n.backend.store_translations 'en', :activemodel => {:attributes => {:"person/addresses" => {:street => 'Person Address Street'}}}
+ assert_equal 'Person Address Street', Person.human_attribute_name('addresses.street')
end
- def test_translated_nested_model_attributes_with_deprecated_lookup_style
- I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:addresses => {:street => 'Street'}}}}
- assert_equal 'Street', Person.human_attribute_name('addresses.street')
+ def test_translated_nested_model_attributes_with_namespace_fallback
+ I18n.backend.store_translations 'en', :activemodel => {:attributes => {:addresses => {:street => 'Cool Address Street'}}}
+ assert_equal 'Cool Address Street', Person.human_attribute_name('addresses.street')
end
def test_translated_model_names

0 comments on commit d4964b3

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