Permalink
Browse files

make ActiveModel::Name fail gracefully with anonymous classes

  • Loading branch information...
1 parent a478389 commit dc39af0a9a998938a969b214554db624dcdd9c85 @qoobaa qoobaa committed Nov 24, 2011
Showing with 16 additions and 0 deletions.
  1. +3 −0 activemodel/lib/active_model/naming.rb
  2. +13 −0 activemodel/test/cases/naming_test.rb
View
3 activemodel/lib/active_model/naming.rb
@@ -12,6 +12,9 @@ class Name < String
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
View
13 activemodel/test/cases/naming_test.rb
@@ -247,3 +247,16 @@ def method_missing(method, *args)
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

0 comments on commit dc39af0

Please sign in to comment.