diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2011-11-24 06:56:46 -0800 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2011-11-24 06:56:46 -0800 |
commit | 30cf3e16a079f833cf396ad9dfaa3a2d4652142d (patch) | |
tree | 239d46f6c8166da67e3e4b2ddd6a1b2bc306f45e | |
parent | a478389b7d70536f1629d080a50a9ecd87c005d0 (diff) | |
parent | dc39af0a9a998938a969b214554db624dcdd9c85 (diff) | |
download | rails-30cf3e16a079f833cf396ad9dfaa3a2d4652142d.tar.gz rails-30cf3e16a079f833cf396ad9dfaa3a2d4652142d.tar.bz2 rails-30cf3e16a079f833cf396ad9dfaa3a2d4652142d.zip |
Merge pull request #3746 from qoobaa/active-model-naming-with-anonymous-classes
make ActiveModel::Name fail gracefully with anonymous classes
-rw-r--r-- | activemodel/lib/active_model/naming.rb | 3 | ||||
-rw-r--r-- | activemodel/test/cases/naming_test.rb | 13 |
2 files changed, 16 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 2566920d63..953d24a3b2 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -12,6 +12,9 @@ module ActiveModel def initialize(klass, namespace = nil, name = nil) name ||= klass.name + + raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if name.blank? + super(name) @unnamespaced = self.sub(/^#{namespace.name}::/, '') if namespace diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index a5ee2d6090..6f9004da7f 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -247,3 +247,16 @@ class NamingHelpersTest < Test::Unit::TestCase ActiveModel::Naming.send(method, *args) end end + +class NameWithAnonymousClassTest < Test::Unit::TestCase + def test_anonymous_class_without_name_argument + assert_raises(ArgumentError) do + model_name = ActiveModel::Name.new(Class.new) + end + end + + def test_anonymous_class_with_name_argument + model_name = ActiveModel::Name.new(Class.new, nil, "Anonymous") + assert_equal "Anonymous", model_name + end +end |