aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-05-20 08:17:11 -0700
committerJosé Valim <jose.valim@gmail.com>2011-05-20 08:17:11 -0700
commit19ee8413deee4bf202e87d53bf6d99b24a795ac8 (patch)
treee1979d7e05c8eeef558439b0fa163e66934e47fd
parent499825e19f961569d6ee72ca4b7e9d6b79164085 (diff)
parente5429b789db242950c39215b4e154bbc73034a28 (diff)
downloadrails-19ee8413deee4bf202e87d53bf6d99b24a795ac8.tar.gz
rails-19ee8413deee4bf202e87d53bf6d99b24a795ac8.tar.bz2
rails-19ee8413deee4bf202e87d53bf6d99b24a795ac8.zip
Merge pull request #1169 from senny/specify_a_custom_active_model_name
Specify the name to be used for ActiveModel::Name fixes #1168
-rw-r--r--activemodel/lib/active_model/naming.rb5
-rw-r--r--activemodel/test/cases/naming_test.rb39
2 files changed, 41 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb
index 74708692af..4c1a82f413 100644
--- a/activemodel/lib/active_model/naming.rb
+++ b/activemodel/lib/active_model/naming.rb
@@ -7,8 +7,9 @@ module ActiveModel
attr_reader :singular, :plural, :element, :collection, :partial_path, :route_key, :param_key, :i18n_key
alias_method :cache_key, :collection
- def initialize(klass, namespace = nil)
- super(klass.name)
+ def initialize(klass, namespace = nil, name = nil)
+ name ||= klass.name
+ super(name)
@unnamespaced = self.sub(/^#{namespace.name}::/, '') if namespace
@klass = klass
diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb
index a7dde2c433..f814fcc56c 100644
--- a/activemodel/test/cases/naming_test.rb
+++ b/activemodel/test/cases/naming_test.rb
@@ -114,6 +114,44 @@ class NamingWithNamespacedModelInSharedNamespaceTest < ActiveModel::TestCase
end
end
+class NamingWithSuppliedModelNameTest < ActiveModel::TestCase
+ def setup
+ @model_name = ActiveModel::Name.new(Blog::Post, nil, 'Article')
+ end
+
+ def test_singular
+ assert_equal 'article', @model_name.singular
+ end
+
+ def test_plural
+ assert_equal 'articles', @model_name.plural
+ end
+
+ def test_element
+ assert_equal 'article', @model_name.element
+ end
+
+ def test_collection
+ assert_equal 'articles', @model_name.collection
+ end
+
+ def test_partial_path
+ assert_equal 'articles/article', @model_name.partial_path
+ end
+
+ def test_human
+ 'Article'
+ end
+
+ def test_route_key
+ assert_equal 'articles', @model_name.route_key
+ end
+
+ def test_param_key
+ assert_equal 'article', @model_name.param_key
+ end
+end
+
class NamingHelpersTest < Test::Unit::TestCase
def setup
@klass = Contact
@@ -171,4 +209,3 @@ class NamingHelpersTest < Test::Unit::TestCase
ActiveModel::Naming.send(method, *args)
end
end
-