Skip to content

Commit 9ff382e

Browse files
erikkessler1vfonic
authored andcommitted
Fix "undefined method `<'" error message (ctran#774)
## Problem I have some files in the "models" directory that are not true a `Class`. For example, a [`dry-types`](https://dry-rb.org/gems/dry-types/1.2/sum/) sum type: ```ruby # app/models/foo.rb Foo = Types::String | Types::Integer ``` This results in the following line when I annotate. ``` Unable to annotate app/models/foo.rb: undefined method `<' for #<Dry::Struct::Sum:0x00007ff2dd262988> ``` Not a blocking issue but somewhat annoying nonetheless. ## Solution When annotating a file, check that the file's object is a `Class` to ensure it has the interface we expect.
1 parent 353781b commit 9ff382e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/annotate/annotate_models.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ def annotate_model_file(annotated, file, header, options)
809809
begin
810810
return false if /#{SKIP_ANNOTATION_PREFIX}.*/ =~ (File.exist?(file) ? File.read(file) : '')
811811
klass = get_model_class(file)
812-
do_annotate = klass &&
812+
do_annotate = klass.is_a?(Class) &&
813813
klass < ActiveRecord::Base &&
814814
(!options[:exclude_sti_subclasses] || !(klass.superclass < ActiveRecord::Base && klass.table_name == klass.superclass.table_name)) &&
815815
!klass.abstract_class? &&

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2726,5 +2726,18 @@ class Foo < ActiveRecord::Base; end
27262726
it 'skips attempt to annotate if no table exists for model' do
27272727
is_expected.to eq nil
27282728
end
2729+
2730+
context 'with a non-class' do
2731+
before do
2732+
NotAClass = 'foo'.freeze # rubocop:disable Naming/ConstantName
2733+
allow(AnnotateModels).to receive(:get_model_class).with('foo.rb') { NotAClass }
2734+
end
2735+
2736+
after { Object.send :remove_const, 'NotAClass' }
2737+
2738+
it "doesn't output an error" do
2739+
expect { subject }.not_to output.to_stderr
2740+
end
2741+
end
27292742
end
27302743
end

0 commit comments

Comments
 (0)