Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

stop relying on side effects of const_missing

  • Loading branch information...
commit 844efb2bb0d6e40a2d830727f6bc235b37c3a1b1 1 parent f38b544
@tenderlove tenderlove authored
View
14 activerecord/lib/active_record/relation/delegation.rb
@@ -77,12 +77,6 @@ def create(klass, *args)
relation_class_for(klass).new(klass, *args)
end
- # This doesn't have to be thread-safe. relation_class_for guarantees that this will only be
- # called exactly once for a given const name.
- def const_missing(name)
- const_set(name, Class.new(self) { include ClassSpecificRelation })
- end
-
private
# Cache the constants in @@subclasses because looking them up via const_get
# make instantiation significantly slower.
@@ -92,7 +86,13 @@ def relation_class_for(klass)
# This hash is keyed by klass.name to avoid memory leaks in development mode
my_cache.compute_if_absent(klass_name) do
# Cache#compute_if_absent guarantees that the block will only executed once for the given klass_name
- const_get("#{name.gsub('::', '_')}_#{klass_name.gsub('::', '_')}", false)
+ subclass_name = "#{name.gsub('::', '_')}_#{klass_name.gsub('::', '_')}"
+
+ if const_defined?(subclass_name)
+ const_get(subclass_name)
+ else
+ const_set(subclass_name, Class.new(self) { include ClassSpecificRelation })
+ end
end
else
ActiveRecord::Relation
View
6 activerecord/test/cases/relation_test.rb
@@ -111,6 +111,12 @@ def test_scope_for_create_is_cached
assert_equal({}, relation.scope_for_create)
end
+ def test_bad_constants_raise_errors
+ assert_raises(NameError) do
+ ActiveRecord::Relation::HelloWorld
+ end
+ end
+
def test_empty_eager_loading?
relation = Relation.new FakeKlass, :b
assert !relation.eager_loading?
Please sign in to comment.
Something went wrong with that request. Please try again.