From 56338b5d3db970b046338fdffaa94039520e5e95 Mon Sep 17 00:00:00 2001 From: Erik Kessler Date: Mon, 9 Mar 2020 09:25:54 -0400 Subject: [PATCH 1/2] Add a broken test --- spec/lib/annotate/annotate_models_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index c3d0069e9..c9cab37af 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -2743,5 +2743,18 @@ class Foo < ActiveRecord::Base; end it 'skips attempt to annotate if no table exists for model' do is_expected.to eq nil end + + context 'with a non-class' do + before do + NotAClass = 'foo'.freeze # rubocop:disable Naming/ConstantName + allow(AnnotateModels).to receive(:get_model_class).with('foo.rb') { NotAClass } + end + + after { Object.send :remove_const, 'NotAClass' } + + it "doesn't output an error" do + expect { subject }.not_to output.to_stderr + end + end end end From 86fd8382ee1e3cec0a9519a41efbaa1a0ca5af9c Mon Sep 17 00:00:00 2001 From: Erik Kessler Date: Mon, 9 Mar 2020 09:26:22 -0400 Subject: [PATCH 2/2] Ensure klass is a Class This checks that the object we are trying to annotate is a Class to ensure it has the interface we expect. --- lib/annotate/annotate_models.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 69da7e862..77cd77335 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -809,7 +809,7 @@ def annotate_model_file(annotated, file, header, options) begin return false if /#{SKIP_ANNOTATION_PREFIX}.*/ =~ (File.exist?(file) ? File.read(file) : '') klass = get_model_class(file) - do_annotate = klass && + do_annotate = klass.is_a?(Class) && klass < ActiveRecord::Base && (!options[:exclude_sti_subclasses] || !(klass.superclass < ActiveRecord::Base && klass.table_name == klass.superclass.table_name)) && !klass.abstract_class? &&