Skip to content

[Fix #281, #413]: Try to get proper loaded model when using Rails eager_load_paths #535

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

Merged
merged 1 commit into from Dec 4, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -711,11 +711,11 @@ def get_model_class(file)
model_path = file.gsub(/\.rb$/, '')
model_dir.each { |dir| model_path = model_path.gsub(/^#{dir}/, '').gsub(/^\//, '') }
begin
get_loaded_model(model_path) || raise(BadModelFileError.new)
get_loaded_model(model_path, file) || raise(BadModelFileError.new)
rescue LoadError
# this is for non-rails projects, which don't get Rails auto-require magic
file_path = File.expand_path(file)
if File.file?(file_path) && silence_warnings { Kernel.require(file_path) }
if File.file?(file_path) && Kernel.require(file_path)
retry
elsif model_path =~ /\//
model_path = model_path.split('/')[1..-1].join('/').to_s
Expand All @@ -726,8 +726,24 @@ def get_model_class(file)
end
end

# Retrieve loaded model class
def get_loaded_model(model_path, file)
loaded_model_class = get_loaded_model_by_path(model_path)
return loaded_model_class if loaded_model_class

# We cannot get loaded model when `model_path` is loaded by Rails
# auto_load/eager_load paths. Try all possible model paths one by one.
absolute_file = File.expand_path(file)
model_paths =
$LOAD_PATH.select { |path| absolute_file.include?(path) }
.map { |path| absolute_file.sub(path, '').sub(/\.rb$/, '').sub(/^\//, '') }
model_paths
.map { |path| get_loaded_model_by_path(path) }
.find { |loaded_model| !loaded_model.nil? }
end

# Retrieve loaded model class by path to the file where it's supposed to be defined.
def get_loaded_model(model_path)
def get_loaded_model_by_path(model_path)
ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.camelize(model_path))
rescue StandardError, LoadError
# Revert to the old way but it is not really robust
Expand Down Expand Up @@ -858,15 +874,6 @@ def classified_sort(cols)
([id] << rest_cols << timestamps << associations).flatten.compact
end

# Ignore warnings for the duration of the block ()
def silence_warnings
old_verbose = $VERBOSE
$VERBOSE = nil
yield
ensure
$VERBOSE = old_verbose
end

private

def with_comments?(klass, options)
Expand Down
26 changes: 22 additions & 4 deletions spec/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1257,11 +1257,29 @@ class LoadedClass < ActiveRecord::Base
EOS
path = File.expand_path('loaded_class', AnnotateModels.model_dir[0])
Kernel.load "#{path}.rb"
expect(Kernel).not_to receive(:require).with(path)
expect(Kernel).not_to receive(:require)

expect(capturing(:stderr) do
check_class_name 'loaded_class.rb', 'LoadedClass'
end).not_to include('warning: already initialized constant LoadedClass::CONSTANT')
end).to be_blank
end

it 'should not require model files twice which is inside a subdirectory' do
dir = Array.new(8) { (0..9).to_a.sample(random: Random.new) }.join
$LOAD_PATH.unshift(File.join(AnnotateModels.model_dir[0], dir))

create "#{dir}/subdir_loaded_class.rb", <<-EOS
class SubdirLoadedClass < ActiveRecord::Base
CONSTANT = 1
end
EOS
path = File.expand_path("#{dir}/subdir_loaded_class", AnnotateModels.model_dir[0])
Kernel.load "#{path}.rb"
expect(Kernel).not_to receive(:require)

expect(capturing(:stderr) do
check_class_name "#{dir}/subdir_loaded_class.rb", 'SubdirLoadedClass'
end).to be_blank
end
end

Expand Down Expand Up @@ -1667,7 +1685,7 @@ class User < ActiveRecord::Base

describe "if a file can't be annotated" do
before do
allow(AnnotateModels).to receive(:get_loaded_model).with('user').and_return(nil)
allow(AnnotateModels).to receive(:get_loaded_model_by_path).with('user').and_return(nil)

write_model('user.rb', <<-EOS)
class User < ActiveRecord::Base
Expand Down Expand Up @@ -1697,7 +1715,7 @@ class User < ActiveRecord::Base

describe "if a file can't be deannotated" do
before do
allow(AnnotateModels).to receive(:get_loaded_model).with('user').and_return(nil)
allow(AnnotateModels).to receive(:get_loaded_model_by_path).with('user').and_return(nil)

write_model('user.rb', <<-EOS)
class User < ActiveRecord::Base
Expand Down