Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix constantize edge case involving prepend, autoloading and name conflicts #27354

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/inflector/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions activesupport/test/autoloading_fixtures/prepend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class SubClassConflict
end

class Prepend
module PrependedModule
end
prepend PrependedModule
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Prepend::SubClassConflict
end
5 changes: 5 additions & 0 deletions activesupport/test/constantize_test_cases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down