From dbe347ff82b5c2715ba598b6b805e91e444b9533 Mon Sep 17 00:00:00 2001 From: Junichi Sato <22004610+sato11@users.noreply.github.com> Date: Thu, 10 Jun 2021 14:41:02 +0900 Subject: [PATCH] `get_loaded_model_by_path` is supposed to be nil-safe This commit partially reverts #801, which by declaring conditional return has turned `get_loaded_model_by_path` to a less safe method that can return nil when its the condition is not met. Apparently the very same condition has been brought to `annotate_model_file` by #774, which seems to cover the "bug" insisted in #801 as well. On the other hand #801 has brought an inconvenient behaviour as well: whenever a non-activerecord model file is found, `get_loaded_model_by_path` returns nil, which leads to raising `BadModelFileError` and ends up printing a bunch of "Unable to annotate ..." messages. Now it seems tests added by #801 are running right and I do not find a problem restoring the previous behaviour and turn it nil-safe again. --- lib/annotate/annotate_models.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 1d84b794..ac214ffb 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -618,9 +618,7 @@ def get_loaded_model(model_path, file) # Retrieve loaded model class by path to the file where it's supposed to be defined. def get_loaded_model_by_path(model_path) - klass = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.camelize(model_path)) - - klass if klass.is_a?(Class) && klass < ActiveRecord::Base + ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.camelize(model_path)) rescue StandardError, LoadError # Revert to the old way but it is not really robust ObjectSpace.each_object(::Class)