@@ -73,7 +73,7 @@ module AnnotateModels
7373
7474 class << self
7575 def model_dir
76- @model_dir || "app/models"
76+ @model_dir . is_a? ( Array ) ? @model_dir : [ @model_dir || "app/models" ]
7777 end
7878
7979 def model_dir = ( dir )
@@ -311,7 +311,7 @@ def annotate(klass, file, header, options={})
311311 did_annotate = false
312312 model_name = klass . name . underscore
313313 table_name = klass . table_name
314- model_file_name = File . join ( model_dir , file )
314+ model_file_name = File . join ( file )
315315
316316 if annotate_one_file ( model_file_name , info , :position_in_class , options_with_position ( options , :position_in_class ) )
317317 did_annotate = true
@@ -357,15 +357,17 @@ def get_model_files(options)
357357 models . reject! { |m | m . match ( /^(.*)=/ ) }
358358 if models . empty?
359359 begin
360- Dir . chdir ( model_dir ) do
361- models = if options [ :ignore_model_sub_dir ]
362- Dir [ "*.rb" ]
363- else
364- Dir [ "**/*.rb" ] . reject { |f | f [ "concerns/" ] }
360+ model_dir . each do |dir |
361+ Dir . chdir ( dir ) do
362+ models . concat ( if options [ :ignore_model_sub_dir ]
363+ Dir [ "*.rb" ] . map { |f | [ dir , f ] }
364+ else
365+ Dir [ "**/*.rb" ] . reject { |f | f [ "concerns/" ] } . map { |f | [ dir , f ] }
366+ end )
365367 end
366368 end
367369 rescue SystemCallError
368- puts "No models found in directory '#{ model_dir } '."
370+ puts "No models found in directory '#{ model_dir . join ( "', '" ) } '."
369371 puts "Either specify models on the command line, or use the --model-dir option."
370372 puts "Call 'annotate --help' for more info."
371373 exit 1
@@ -379,11 +381,12 @@ def get_model_files(options)
379381 # in subdirectories without namespacing.
380382 def get_model_class ( file )
381383 model_path = file . gsub ( /\. rb$/ , '' )
384+ model_dir . each { |dir | model_path = model_path . gsub ( /^#{ dir } / , '' ) . gsub ( /^\/ / , '' ) }
382385 begin
383386 get_loaded_model ( model_path ) or raise LoadError . new ( "cannot load a model from #{ file } " )
384387 rescue LoadError
385388 # this is for non-rails projects, which don't get Rails auto-require magic
386- if Kernel . require ( File . expand_path ( " #{ model_dir } / #{ model_path } " ) )
389+ if Kernel . require ( file )
387390 retry
388391 elsif model_path . match ( /\/ / )
389392 model_path = model_path . split ( '/' ) [ 1 ..-1 ] . join ( '/' ) . to_s
@@ -428,10 +431,10 @@ def do_annotations(options={})
428431
429432 annotated = [ ]
430433 get_model_files ( options ) . each do |file |
431- annotate_model_file ( annotated , file , header , options )
434+ annotate_model_file ( annotated , File . join ( file ) , header , options )
432435 end
433436 if annotated . empty?
434- puts "Nothing annotated ."
437+ puts "Nothing to annotate ."
435438 else
436439 puts "Annotated (#{ annotated . length } ): #{ annotated . join ( ', ' ) } "
437440 end
@@ -456,12 +459,13 @@ def remove_annotations(options={})
456459 deannotated = [ ]
457460 deannotated_klass = false
458461 get_model_files ( options ) . each do |file |
462+ file = File . join ( file )
459463 begin
460464 klass = get_model_class ( file )
461465 if klass < ActiveRecord ::Base && !klass . abstract_class?
462466 model_name = klass . name . underscore
463467 table_name = klass . table_name
464- model_file_name = File . join ( model_dir , file )
468+ model_file_name = file
465469 deannotated_klass = true if ( remove_annotation_of_file ( model_file_name ) )
466470
467471 ( TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS ) .
@@ -475,7 +479,7 @@ def remove_annotations(options={})
475479 end
476480 deannotated << klass if ( deannotated_klass )
477481 rescue Exception => e
478- puts "Unable to deannotate #{ file } : #{ e . message } "
482+ puts "Unable to deannotate #{ File . join ( file ) } : #{ e . message } "
479483 puts "\t " + e . backtrace . join ( "\n \t " ) if options [ :trace ]
480484 end
481485 end
0 commit comments