1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

stop relying on side effects of const_missing

This commit is contained in:
Aaron Patterson 2013-07-23 15:02:43 -07:00
parent f38b544442
commit 844efb2bb0
2 changed files with 13 additions and 7 deletions

View file

@ -77,12 +77,6 @@ module ActiveRecord
relation_class_for(klass).new(klass, *args) relation_class_for(klass).new(klass, *args)
end 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 private
# Cache the constants in @@subclasses because looking them up via const_get # Cache the constants in @@subclasses because looking them up via const_get
# make instantiation significantly slower. # make instantiation significantly slower.
@ -92,7 +86,13 @@ module ActiveRecord
# This hash is keyed by klass.name to avoid memory leaks in development mode # This hash is keyed by klass.name to avoid memory leaks in development mode
my_cache.compute_if_absent(klass_name) do 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 # 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 end
else else
ActiveRecord::Relation ActiveRecord::Relation

View file

@ -111,6 +111,12 @@ module ActiveRecord
assert_equal({}, relation.scope_for_create) assert_equal({}, relation.scope_for_create)
end end
def test_bad_constants_raise_errors
assert_raises(NameError) do
ActiveRecord::Relation::HelloWorld
end
end
def test_empty_eager_loading? def test_empty_eager_loading?
relation = Relation.new FakeKlass, :b relation = Relation.new FakeKlass, :b
assert !relation.eager_loading? assert !relation.eager_loading?