From 75a960ca6e38ae68fd55c034272102077fd95afa Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 12 Aug 2010 11:43:10 -0700 Subject: [PATCH] Don't act destructively on ActiveModel::Name#human options hash. [#5366 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activemodel/lib/active_model/naming.rb | 5 +++-- activemodel/test/cases/naming_test.rb | 4 ++++ activemodel/test/cases/translation_test.rb | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 0706df4b590..fc2abacb6d6 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/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 @@ -35,10 +36,10 @@ module ActiveModel 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 diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index c6b663ef93d..40ce4c0e2d0 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -28,6 +28,10 @@ class NamingTest < ActiveModel::TestCase 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 diff --git a/activemodel/test/cases/translation_test.rb b/activemodel/test/cases/translation_test.rb index d6942a54757..ac2e56321ea 100644 --- a/activemodel/test/cases/translation_test.rb +++ b/activemodel/test/cases/translation_test.rb @@ -46,5 +46,11 @@ class ActiveModelI18nTests < ActiveModel::TestCase 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