Actually always inject autoextend into rails autoloaders

Change-Id: Ib2fdd0d82810e2ecc6de29b79b401c163a206189
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/277135
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Ethan Vizitei <evizitei@instructure.com>
QA-Review: Jacob Burroughs <jburroughs@instructure.com>
Product-Review: Jacob Burroughs <jburroughs@instructure.com>
This commit is contained in:
Jacob Burroughs 2021-10-29 12:16:08 -05:00
parent f7c28bbfa4
commit 4378cca0e4
1 changed files with 7 additions and 6 deletions

View File

@ -36,7 +36,10 @@ module Autoextend
if recursive if recursive
const.constants(false).each do |child| const.constants(false).each do |child|
next if const.autoload?(child) # If the constant is set to autoload, don't accidentally autoload it
# If we are processing a constant's parent while processing the child
# the constant will be return by constants, but not defined, so just skip it
next if const.autoload?(child) || !const.const_defined?(child, false)
child_const = const.const_get(child, false) child_const = const.const_get(child, false)
next unless child_const.is_a?(Module) next unless child_const.is_a?(Module)
@ -135,12 +138,10 @@ module Autoextend
end end
def inject_into_zetwerk def inject_into_zetwerk
return unless Object.const_defined?(:Zeitwerk) return unless Object.const_defined?(:Rails)
Zeitwerk::Registry.loaders.each do |loader| Rails.autoloaders.each do |loader|
loader.on_load do |_cpath, value, abspath| loader.on_load do |_cpath, value, _abspath|
# Skip autovivified modules from directories
next unless abspath.end_with?('.rb')
next unless value.is_a?(Module) next unless value.is_a?(Module)
Autoextend.const_added(value, source: :Zeitwerk, recursive: true) Autoextend.const_added(value, source: :Zeitwerk, recursive: true)