Skip to content

Commit

Permalink
Add :ignore_unknown_models option to disable warnings about invalid m…
Browse files Browse the repository at this point in the history
…odel files, #251
  • Loading branch information
ctran committed Dec 30, 2015
1 parent 6523dd7 commit a1cc2a0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions bin/annotate
Expand Up @@ -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? })
Expand Down
2 changes: 1 addition & 1 deletion lib/annotate.rb
Expand Up @@ -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,
Expand Down
13 changes: 12 additions & 1 deletion lib/annotate/annotate_models.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Up @@ -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',
Expand Down

0 comments on commit a1cc2a0

Please sign in to comment.