annotate_models icon indicating copy to clipboard operation
annotate_models copied to clipboard

get_model_class file doesn't contain a valid model class

Open awoodworth opened this issue 7 years ago • 5 comments

To begin with, I was testing out adding annotation to my models. After reviewing what all gets added, I wanted to remove the changes using the tool as well... but got this error:

/usr/local/bundle/gems/annotate-2.7.4/lib/annotate/annotate_models.rb:714:in `get_model_class': file doesn't contain a valid model class (AnnotateModels::BadModelFileError)

I feel like it could be more specific as I was unable to initially determine what was going on. I looked further up in the console, and I noticed a warning when running the initial annotate command.

Unable to annotate app/models/csc.rb: file doesn't contain a valid model class

I then went here to see the error/why it was happening. Since the library determines the class name via the file name using ActiveSupport::Inflector, I was able to figure out that I missed adding CSC as an acronym.

Suggested Enhancement: I believe remove_annotations is executed when we pass --delete as an option (based on backtrace). Since get_model_class can potentially raise BadModelFileError, I think we need to add LoadError to this line.

Example:

rescue StandardError, LoadError => e
  $stderr.puts "Unable to deannotate #{File.join(file)}: #{e.message}"
  $stderr.puts "\t" + e.backtrace.join("\n\t") if options[:trace]
end

Commands

annotate --delete

backtrace:

/usr/local/bundle/gems/annotate-2.7.4/lib/annotate/annotate_models.rb:714:in `get_model_class': file doesn't contain a valid model class (AnnotateModels::BadModelFileError)
        from /usr/local/bundle/gems/annotate-2.7.4/lib/annotate/annotate_models.rb:823:in `block in remove_annotations'
        from /usr/local/bundle/gems/annotate-2.7.4/lib/annotate/annotate_models.rb:820:in `each'
        from /usr/local/bundle/gems/annotate-2.7.4/lib/annotate/annotate_models.rb:820:in `remove_annotations'
        from /usr/local/bundle/gems/annotate-2.7.4/bin/annotate:211:in `<top (required)>'
        from /usr/local/bundle/bin/annotate:23:in `load'
        from /usr/local/bundle/bin/annotate:23:in `<main>'

Version

  • annotate version: 2.7.4
  • rails version: 5.2.1
  • ruby version: 2.4.4

awoodworth avatar Oct 11 '18 00:10 awoodworth

I ran into this same error as well.

ndiesslin avatar Jun 04 '19 20:06 ndiesslin

@awoodworth @ndiesslin are yall still encountering this problem with later versions of the gem?

drwl avatar Feb 17 '20 02:02 drwl

@drwl I ended up manually going through files to remove the annotations, which isn't ideal. If it would help I could install again with a later version and test it out.

ndiesslin avatar Feb 17 '20 15:02 ndiesslin

@drwl it looks like this is still happening with the later versions of the gem. Also, my environment is a little older so this could potentially be part of the issue as well.

Command: annotate --models --d

Error: /usr/local/bundle/gems/annotate-3.0.3/lib/annotate/annotate_models.rb:729:in get_model_class': file doesn't contain a valid model class (AnnotateModels::BadModelFileError)`

Version

  • annotate version: 3.0.3
  • rails version: 4.2.11
  • ruby version: 2.3.0

ndiesslin avatar Feb 17 '20 16:02 ndiesslin

I got the same error.

Error:

.rvm/gems/ruby-2.4.0/gems/annotate-3.2.0/lib/annotate/annotate_models.rb:585:in `get_model_class': file doesn't contain a valid model class (AnnotateModels::BadModelFileError)

command:

annotate --delete

Version:

annotate version: 3.2.0 ruby version: 2.4.0 rails version: 4.2.8

bhartee-sahare avatar Dec 29 '22 12:12 bhartee-sahare