Skip to content

Commit

Permalink
Don't act destructively on ActiveModel::Name#human options hash. [#5366
Browse files Browse the repository at this point in the history
… state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
John Firebaugh authored and josevalim committed Sep 25, 2010
1 parent 308517e commit 75a960c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions activemodel/lib/active_model/naming.rb
@@ -1,4 +1,5 @@
require 'active_support/inflector'
require 'active_support/core_ext/hash/except'

module ActiveModel
class Name < String
Expand Down Expand Up @@ -35,10 +36,10 @@ def human(options={})
klass.model_name.i18n_key
end

defaults << options.delete(:default) if options[:default]
defaults << options[:default] if options[:default]
defaults << @human

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

Expand Down
4 changes: 4 additions & 0 deletions activemodel/test/cases/naming_test.rb
Expand Up @@ -28,6 +28,10 @@ def test_collection
def test_partial_path
assert_equal 'post/track_backs/track_back', @model_name.partial_path
end

def test_human
assert_equal 'Track back', @model_name.human
end
end

class NamingWithNamespacedModelInIsolatedNamespaceTest < ActiveModel::TestCase
Expand Down
6 changes: 6 additions & 0 deletions activemodel/test/cases/translation_test.rb
Expand Up @@ -46,5 +46,11 @@ def test_translated_model_names_with_ancestors_fallback
I18n.backend.store_translations 'en', :activemodel => {:models => {:person => 'person model'} }
assert_equal 'person model', Child.model_name.human
end

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

0 comments on commit 75a960c

Please sign in to comment.