Skip to content

Commit

Permalink
Merge pull request rails#6035 from carlosantoniodasilva/amo-translati…
Browse files Browse the repository at this point in the history
…on-refactor

Refactor AMo::Translation, avoid changing options in human attr name
  • Loading branch information
josevalim committed Apr 28, 2012
2 parents 9fc9e89 + f48d83b commit 71cf6ef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/naming.rb
Expand Up @@ -54,7 +54,7 @@ def human(options={})
defaults << options[:default] if options[:default]
defaults << @human

options = {:scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults}.merge(options.except(:default))
options = { :scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults }.merge!(options.except(:default))
I18n.translate(defaults.shift, options)
end

Expand Down
17 changes: 8 additions & 9 deletions activemodel/lib/active_model/translation.rb
@@ -1,5 +1,3 @@
require 'active_support/core_ext/hash/reverse_merge'

module ActiveModel

# == Active Model Translation
Expand Down Expand Up @@ -43,27 +41,28 @@ def lookup_ancestors
#
# Specify +options+ with additional translating options.
def human_attribute_name(attribute, options = {})
defaults = []
options = { :count => 1 }.merge!(options)
parts = attribute.to_s.split(".", 2)
attribute = parts.pop
namespace = parts.pop
attributes_scope = "#{self.i18n_scope}.attributes"

if namespace
lookup_ancestors.each do |klass|
defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}"
defaults = lookup_ancestors.map do |klass|
:"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}"
end
defaults << :"#{self.i18n_scope}.attributes.#{namespace}.#{attribute}"
defaults << :"#{attributes_scope}.#{namespace}.#{attribute}"
else
lookup_ancestors.each do |klass|
defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}"
defaults = lookup_ancestors.map do |klass|
:"#{attributes_scope}.#{klass.model_name.i18n_key}.#{attribute}"
end
end

defaults << :"attributes.#{attribute}"
defaults << options.delete(:default) if options[:default]
defaults << attribute.humanize

options.reverse_merge! :count => 1, :default => defaults
options[:default] = defaults
I18n.translate(defaults.shift, options)
end
end
Expand Down
10 changes: 8 additions & 2 deletions activemodel/test/cases/translation_test.rb
Expand Up @@ -82,9 +82,15 @@ def test_translated_model_names_with_ancestors_fallback
end

def test_human_does_not_modify_options
options = {:default => 'person model'}
options = { :default => 'person model' }
Person.model_name.human(options)
assert_equal({:default => 'person model'}, options)
assert_equal({ :default => 'person model' }, options)
end

def test_human_attribute_name_does_not_modify_options
options = { :default => 'Cool gender' }
Person.human_attribute_name('gender', options)
assert_equal({ :default => 'Cool gender' }, options)
end
end

0 comments on commit 71cf6ef

Please sign in to comment.