diff --git a/bin/annotate b/bin/annotate index e4a298d0e..24a8f9cca 100755 --- a/bin/annotate +++ b/bin/annotate @@ -182,6 +182,10 @@ OptionParser.new do |opts| ENV['hide_limit_column_types'] = "#{values}" end + opts.on('--ignore-unknown-models', "don't display warnings for bad model files" ) do |values| + ENV['ignore_unknown_models'] = "true" + end + end.parse! options = Annotate.setup_options({ :is_rake => ENV['is_rake'] && !ENV['is_rake'].empty? }) diff --git a/lib/annotate.rb b/lib/annotate.rb index c4e7da416..fa9b6607e 100755 --- a/lib/annotate.rb +++ b/lib/annotate.rb @@ -27,7 +27,7 @@ module Annotate :exclude_fixtures, :exclude_factories, :ignore_model_sub_dir, :format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace, :timestamp, :exclude_serializers, :classified_sort, :show_foreign_keys, - :exclude_scaffolds, :exclude_controllers, :exclude_helpers + :exclude_scaffolds, :exclude_controllers, :exclude_helpers, :ignore_unknown_models, ] OTHER_OPTIONS=[ :ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper, :routes, diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 5ddb31d60..72c0a1c5e 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -488,7 +488,7 @@ def get_model_class(file) model_path = file.gsub(/\.rb$/, '') model_dir.each { |dir| model_path = model_path.gsub(/^#{dir}/, '').gsub(/^\//, '') } begin - get_loaded_model(model_path) or raise LoadError.new("cannot load a model from #{file}") + get_loaded_model(model_path) or raise BadModelFileError.new rescue LoadError # this is for non-rails projects, which don't get Rails auto-require magic file_path = File.expand_path(file) @@ -556,6 +556,11 @@ def annotate_model_file(annotated, file, header, options) annotated << file end end + rescue BadModelFileError => e + unless options[:ignore_unknown_models] + puts "Unable to annotate #{file}: #{e.message}" + puts "\t" + e.backtrace.join("\n\t") if options[:trace] + end rescue Exception => e puts "Unable to annotate #{file}: #{e.message}" puts "\t" + e.backtrace.join("\n\t") if options[:trace] @@ -632,4 +637,10 @@ def silence_warnings $VERBOSE = old_verbose end end + + class BadModelFileError < LoadError + def to_s + "file doesn't contain a valid model class" + end + end end diff --git a/lib/generators/annotate/templates/auto_annotate_models.rake b/lib/generators/annotate/templates/auto_annotate_models.rake index c75ee7979..fc9c2d6a7 100644 --- a/lib/generators/annotate/templates/auto_annotate_models.rake +++ b/lib/generators/annotate/templates/auto_annotate_models.rake @@ -29,6 +29,7 @@ if Rails.env.development? 'exclude_helpers' => 'false', 'ignore_model_sub_dir' => 'false', 'ignore_columns' => nil, + 'ignore_unknown_models' => 'false', 'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(',') %>', 'skip_on_db_migrate' => 'false', 'format_bare' => 'true',