Skip to content

Commit 41d13a2

Browse files
committed
Add posibility to define model_dir as an array of directorie paths
1 parent c40faf4 commit 41d13a2

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

README.rdoc

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ you can do so with a simple environment variable, instead of editing the
176176
-m, --show-migration Include the migration version number in the annotation
177177
-i, --show-indexes List the table's database indexes in the annotation
178178
-s, --simple-indexes Concat the column's related indexes in the annotation
179-
--model-dir dir Annotate model files stored in dir rather than app/models
179+
--model-dir dir Annotate model files stored in dir rather than app/models, separate multiple dirs with comas
180180
--ignore-model-subdirects Ignore subdirectories of the models directory
181181
--sort Sort columns alphabetically, rather than in creation order
182182
-R, --require path Additional file to require before loading models, may be used multiple times

bin/annotate

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ OptionParser.new do |opts|
120120
end
121121

122122
opts.on('--model-dir dir',
123-
"Annotate model files stored in dir rather than app/models") do |dir|
123+
"Annotate model files stored in dir rather than app/models, separate multiple dirs with comas") do |dir|
124124
ENV['model_dir'] = dir
125125
end
126126

lib/annotate.rb

100644100755
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ module Annotate
2727
:timestamp, :exclude_serializers
2828
]
2929
OTHER_OPTIONS=[
30-
:model_dir, :ignore_columns
30+
:ignore_columns
3131
]
3232
PATH_OPTIONS=[
33-
:require,
33+
:require, :model_dir
3434
]
3535

3636

@@ -70,7 +70,7 @@ def self.setup_options(options = {})
7070
end
7171

7272
if(!options[:model_dir])
73-
options[:model_dir] = 'app/models'
73+
options[:model_dir] = ['app/models']
7474
end
7575

7676
return options
@@ -111,8 +111,10 @@ def self.eager_load(options)
111111
klass.eager_load!
112112
end
113113
else
114-
FileList["#{options[:model_dir]}/**/*.rb"].each do |fname|
115-
require File.expand_path(fname)
114+
options[:model_dir].each do |dir|
115+
FileList["#{dir}/**/*.rb"].each do |fname|
116+
require File.expand_path(fname)
117+
end
116118
end
117119
end
118120
end

lib/annotate/annotate_models.rb

100644100755
Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/tasks/annotate_models.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ task :annotate_models => :environment do
1818
options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position'])
1919
options[:show_indexes] = Annotate.true?(ENV['show_indexes'])
2020
options[:simple_indexes] = Annotate.true?(ENV['simple_indexes'])
21-
options[:model_dir] = ENV['model_dir']
21+
options[:model_dir] = ENV['model_dir'] ? ENV['model_dir'].split(',') : []
2222
options[:include_version] = Annotate.true?(ENV['include_version'])
2323
options[:require] = ENV['require'] ? ENV['require'].split(',') : []
2424
options[:exclude_tests] = Annotate.true?(ENV['exclude_tests'])

0 commit comments

Comments
 (0)