2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
module Mixin
|
|
|
|
module RequiredSubPaths
|
|
|
|
|
|
|
|
def self.included(_base)
|
2017-11-23 08:09:44 +00:00
|
|
|
path = caller_locations(1..1).first.path
|
2017-08-14 11:56:23 +00:00
|
|
|
sub_path = File.join(File.dirname(path), File.basename(path, '.rb'))
|
|
|
|
eager_load_recursive(sub_path)
|
|
|
|
end
|
|
|
|
|
2021-06-23 11:35:27 +00:00
|
|
|
# Loads a directory recursively. This can be needed when accessing
|
|
|
|
# modules not directly via .constantize on a known string, but dynamically
|
|
|
|
# via the inheritance tree, e.g. via .descendants (which assumes they have
|
|
|
|
# previously been loaded).
|
2017-08-14 11:56:23 +00:00
|
|
|
def self.eager_load_recursive(path)
|
|
|
|
|
|
|
|
excluded = ['.', '..']
|
|
|
|
sub_paths = []
|
|
|
|
Dir.entries(path).each do |entry|
|
|
|
|
next if excluded.include?(entry)
|
|
|
|
|
|
|
|
sub_path = File.join(path, entry)
|
|
|
|
|
|
|
|
if File.directory?(sub_path)
|
|
|
|
sub_paths.push(sub_path)
|
2021-05-12 11:37:44 +00:00
|
|
|
elsif sub_path =~ %r{\A(.*)\.rb\z}
|
2017-08-14 11:56:23 +00:00
|
|
|
require_path = $1
|
2019-04-06 15:22:38 +00:00
|
|
|
require_dependency(require_path)
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sub_paths.each do |sub_path|
|
|
|
|
eager_load_recursive(sub_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|