Skip to content

Commit

Permalink
[ctran#969] Add support for Rails autoloader collapsed directories
Browse files Browse the repository at this point in the history
  • Loading branch information
bazay committed Oct 4, 2022
1 parent ae0b79c commit d145e4c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/annotate/annotate_models.rb
Expand Up @@ -611,6 +611,15 @@ def get_loaded_model(model_path, file)
$LOAD_PATH.map(&:to_s)
.select { |path| absolute_file.include?(path) }
.map { |path| absolute_file.sub(path, '').sub(/\.rb$/, '').sub(/^\//, '') }

# Handle Rails apps with collapsed model paths
model_paths = model_paths
.select do |model_path|
defined?(Rails) &&
Rails.autoloaders.main.collapse_dirs.any? &&
Rails.autoloaders.main.collapse_dirs.select { |path| path.match(model_path) }
end.map { |model_path| model_path.sub(/\/models/, '') }

model_paths
.map { |path| get_loaded_model_by_path(path) }
.find { |loaded_model| !loaded_model.nil? }
Expand Down

0 comments on commit d145e4c

Please sign in to comment.