annotate_models
annotate_models copied to clipboard
get_model_class file doesn't contain a valid model class
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
I ran into this same error as well.
@awoodworth @ndiesslin are yall still encountering this problem with later versions of the gem?
@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.
@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
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