Skip to content

Commit

Permalink
Merge pull request rails#3859 from kuroda/human_attribute_name
Browse files Browse the repository at this point in the history
Fix human_attribute_name to handle names with dots
  • Loading branch information
josevalim committed Dec 5, 2011
2 parents be482bd + dff19f7 commit 2985151
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 6 additions & 2 deletions activemodel/lib/active_model/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ def lookup_ancestors
#
# Specify +options+ with additional translating options.
def human_attribute_name(attribute, options = {})
defaults = lookup_ancestors.map do |klass|
:"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}"
defaults = []
lookup_ancestors.each do |klass|
if attribute.match(/\./)
defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute.gsub(/\./, '/')}"
end
defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}"
end

defaults << :"attributes.#{attribute}"
Expand Down
10 changes: 10 additions & 0 deletions activemodel/test/cases/translation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ def test_translated_model_attributes_with_attribute_matching_namespaced_model_na
assert_equal 'person gender attribute', Person::Gender.human_attribute_name('attribute')
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')
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')
end

def test_translated_model_names
I18n.backend.store_translations 'en', :activemodel => {:models => {:person => 'person model'} }
assert_equal 'person model', Person.model_name.human
Expand Down

0 comments on commit 2985151

Please sign in to comment.