Skip to content

Commit a1cc2a0

Browse files
committed
Add :ignore_unknown_models option to disable warnings about invalid model files, #251
1 parent 6523dd7 commit a1cc2a0

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

bin/annotate

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ OptionParser.new do |opts|
182182
ENV['hide_limit_column_types'] = "#{values}"
183183
end
184184

185+
opts.on('--ignore-unknown-models', "don't display warnings for bad model files" ) do |values|
186+
ENV['ignore_unknown_models'] = "true"
187+
end
188+
185189
end.parse!
186190

187191
options = Annotate.setup_options({ :is_rake => ENV['is_rake'] && !ENV['is_rake'].empty? })

lib/annotate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module Annotate
2727
:exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
2828
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
2929
:timestamp, :exclude_serializers, :classified_sort, :show_foreign_keys,
30-
:exclude_scaffolds, :exclude_controllers, :exclude_helpers
30+
:exclude_scaffolds, :exclude_controllers, :exclude_helpers, :ignore_unknown_models,
3131
]
3232
OTHER_OPTIONS=[
3333
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper, :routes,

lib/annotate/annotate_models.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def get_model_class(file)
488488
model_path = file.gsub(/\.rb$/, '')
489489
model_dir.each { |dir| model_path = model_path.gsub(/^#{dir}/, '').gsub(/^\//, '') }
490490
begin
491-
get_loaded_model(model_path) or raise LoadError.new("cannot load a model from #{file}")
491+
get_loaded_model(model_path) or raise BadModelFileError.new
492492
rescue LoadError
493493
# this is for non-rails projects, which don't get Rails auto-require magic
494494
file_path = File.expand_path(file)
@@ -556,6 +556,11 @@ def annotate_model_file(annotated, file, header, options)
556556
annotated << file
557557
end
558558
end
559+
rescue BadModelFileError => e
560+
unless options[:ignore_unknown_models]
561+
puts "Unable to annotate #{file}: #{e.message}"
562+
puts "\t" + e.backtrace.join("\n\t") if options[:trace]
563+
end
559564
rescue Exception => e
560565
puts "Unable to annotate #{file}: #{e.message}"
561566
puts "\t" + e.backtrace.join("\n\t") if options[:trace]
@@ -632,4 +637,10 @@ def silence_warnings
632637
$VERBOSE = old_verbose
633638
end
634639
end
640+
641+
class BadModelFileError < LoadError
642+
def to_s
643+
"file doesn't contain a valid model class"
644+
end
645+
end
635646
end

lib/generators/annotate/templates/auto_annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ if Rails.env.development?
2929
'exclude_helpers' => 'false',
3030
'ignore_model_sub_dir' => 'false',
3131
'ignore_columns' => nil,
32+
'ignore_unknown_models' => 'false',
3233
'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(',') %>',
3334
'skip_on_db_migrate' => 'false',
3435
'format_bare' => 'true',

0 commit comments

Comments
 (0)