Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make ActiveModel::Name fail gracefully with anonymous classes

  • Loading branch information...
commit dc39af0a9a998938a969b214554db624dcdd9c85 1 parent a478389
@qoobaa qoobaa authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.