Skip to content

Commit 8372674

Browse files
committed
Merge remote-tracking branch 'upstream/pr/970' into community-maintained
2 parents 5d01c41 + 67cc110 commit 8372674

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/annotate/annotate_models.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,15 @@ def get_loaded_model(model_path, file)
669669
$LOAD_PATH.map(&:to_s)
670670
.select { |path| absolute_file.include?(path) }
671671
.map { |path| absolute_file.sub(path, '').sub(/\.rb$/, '').sub(/^\//, '') }
672+
673+
# Handle Rails apps with collapsed model paths
674+
model_paths = model_paths
675+
.select do |mpath|
676+
defined?(Rails) &&
677+
Rails.autoloaders.main.collapse_dirs.any? &&
678+
Rails.autoloaders.main.collapse_dirs.select { |path| path.match(mpath) }
679+
end.map { |mpath| mpath.sub(/\/models/, '') }
680+
672681
model_paths
673682
.map { |path| get_loaded_model_by_path(path) }
674683
.find { |loaded_model| !loaded_model.nil? }

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,37 @@ class FooInsideBar < ActiveRecord::Base
21982198
expect(klass.name).to eq('Bar::FooInsideBar')
21992199
end
22002200
end
2201+
2202+
context 'when class Bar::Foo is defined in Rails collapsed directory' do
2203+
before do
2204+
Rails = double('Rails')
2205+
# rubocop:disable RSpec/MessageChain
2206+
allow(Rails).to receive_message_chain(:autoloaders, :main, :collapse_dirs) { Set.new(["bar/models"]) }
2207+
# rubocop:enable RSpec/MessageChain
2208+
$LOAD_PATH.unshift(dir)
2209+
end
2210+
2211+
let :dir do
2212+
AnnotateModels.model_dir[0]
2213+
end
2214+
2215+
let :filename do
2216+
'bar/models/foo.rb'
2217+
end
2218+
2219+
let :file_content do
2220+
<<~EOS
2221+
module Bar
2222+
class Foo < ActiveRecord::Base
2223+
end
2224+
end
2225+
EOS
2226+
end
2227+
2228+
it 'works' do
2229+
expect(klass.name).to eql('Bar::Foo')
2230+
end
2231+
end
22012232
end
22022233

22032234
context 'when class is defined inside module and class name is not capitalized normally' do

0 commit comments

Comments
 (0)