Merge pull request #27354 from Shopify/fix-constantize-and-prepended-modules

Fix constantize edge case involving prepend, autoloading and name conflicts
This commit is contained in:
Rafael França 2016-12-14 12:31:09 -05:00 committed by GitHub
commit 4ae8595ca2
4 changed files with 16 additions and 1 deletions

@ -274,7 +274,7 @@ def constantize(camel_cased_word)
# Go down the ancestors to check if it is owned directly. The check
# stops when we reach Object or the end of ancestors tree.
constant = constant.ancestors.inject do |const, ancestor|
constant = constant.ancestors.inject(constant) do |const, ancestor|
break const if ancestor == Object
break ancestor if ancestor.const_defined?(name, false)
const

@ -0,0 +1,8 @@
class SubClassConflict
end
class Prepend
module PrependedModule
end
prepend PrependedModule
end

@ -0,0 +1,2 @@
class Prepend::SubClassConflict
end

@ -73,6 +73,11 @@ def run_constantize_tests_on
yield("RaisesNoMethodError")
end
end
with_autoloading_fixtures do
yield("Prepend::SubClassConflict")
assert_equal "constant", defined?(Prepend::SubClassConflict)
end
end
def run_safe_constantize_tests_on