Commit 41d13a25 by capripot

Add posibility to define model_dir as an array of directorie paths

parent c40faf4a
...@@ -176,7 +176,7 @@ you can do so with a simple environment variable, instead of editing the ...@@ -176,7 +176,7 @@ you can do so with a simple environment variable, instead of editing the
-m, --show-migration Include the migration version number in the annotation -m, --show-migration Include the migration version number in the annotation
-i, --show-indexes List the table's database indexes in the annotation -i, --show-indexes List the table's database indexes in the annotation
-s, --simple-indexes Concat the column's related indexes in the annotation -s, --simple-indexes Concat the column's related indexes in the annotation
--model-dir dir Annotate model files stored in dir rather than app/models --model-dir dir Annotate model files stored in dir rather than app/models, separate multiple dirs with comas
--ignore-model-subdirects Ignore subdirectories of the models directory --ignore-model-subdirects Ignore subdirectories of the models directory
--sort Sort columns alphabetically, rather than in creation order --sort Sort columns alphabetically, rather than in creation order
-R, --require path Additional file to require before loading models, may be used multiple times -R, --require path Additional file to require before loading models, may be used multiple times
......
...@@ -120,7 +120,7 @@ OptionParser.new do |opts| ...@@ -120,7 +120,7 @@ OptionParser.new do |opts|
end end
opts.on('--model-dir dir', opts.on('--model-dir dir',
"Annotate model files stored in dir rather than app/models") do |dir| "Annotate model files stored in dir rather than app/models, separate multiple dirs with comas") do |dir|
ENV['model_dir'] = dir ENV['model_dir'] = dir
end end
......
...@@ -27,10 +27,10 @@ module Annotate ...@@ -27,10 +27,10 @@ module Annotate
:timestamp, :exclude_serializers :timestamp, :exclude_serializers
] ]
OTHER_OPTIONS=[ OTHER_OPTIONS=[
:model_dir, :ignore_columns :ignore_columns
] ]
PATH_OPTIONS=[ PATH_OPTIONS=[
:require, :require, :model_dir
] ]
...@@ -70,7 +70,7 @@ module Annotate ...@@ -70,7 +70,7 @@ module Annotate
end end
if(!options[:model_dir]) if(!options[:model_dir])
options[:model_dir] = 'app/models' options[:model_dir] = ['app/models']
end end
return options return options
...@@ -111,8 +111,10 @@ module Annotate ...@@ -111,8 +111,10 @@ module Annotate
klass.eager_load! klass.eager_load!
end end
else else
FileList["#{options[:model_dir]}/**/*.rb"].each do |fname| options[:model_dir].each do |dir|
require File.expand_path(fname) FileList["#{dir}/**/*.rb"].each do |fname|
require File.expand_path(fname)
end
end end
end end
end end
......
...@@ -73,7 +73,7 @@ module AnnotateModels ...@@ -73,7 +73,7 @@ module AnnotateModels
class << self class << self
def model_dir def model_dir
@model_dir || "app/models" @model_dir.is_a?(Array) ? @model_dir : [@model_dir || "app/models"]
end end
def model_dir=(dir) def model_dir=(dir)
...@@ -311,7 +311,7 @@ module AnnotateModels ...@@ -311,7 +311,7 @@ module AnnotateModels
did_annotate = false did_annotate = false
model_name = klass.name.underscore model_name = klass.name.underscore
table_name = klass.table_name table_name = klass.table_name
model_file_name = File.join(model_dir, file) model_file_name = File.join(file)
if annotate_one_file(model_file_name, info, :position_in_class, options_with_position(options, :position_in_class)) if annotate_one_file(model_file_name, info, :position_in_class, options_with_position(options, :position_in_class))
did_annotate = true did_annotate = true
...@@ -357,15 +357,17 @@ module AnnotateModels ...@@ -357,15 +357,17 @@ module AnnotateModels
models.reject!{|m| m.match(/^(.*)=/)} models.reject!{|m| m.match(/^(.*)=/)}
if models.empty? if models.empty?
begin begin
Dir.chdir(model_dir) do model_dir.each do |dir|
models = if options[:ignore_model_sub_dir] Dir.chdir(dir) do
Dir["*.rb"] models.concat( if options[:ignore_model_sub_dir]
else Dir["*.rb"].map{ |f| [dir, f] }
Dir["**/*.rb"].reject{ |f| f["concerns/"] } else
Dir["**/*.rb"].reject{ |f| f["concerns/"] }.map{ |f| [dir, f] }
end)
end end
end end
rescue SystemCallError rescue SystemCallError
puts "No models found in directory '#{model_dir}'." puts "No models found in directory '#{model_dir.join("', '")}'."
puts "Either specify models on the command line, or use the --model-dir option." puts "Either specify models on the command line, or use the --model-dir option."
puts "Call 'annotate --help' for more info." puts "Call 'annotate --help' for more info."
exit 1 exit 1
...@@ -379,11 +381,12 @@ module AnnotateModels ...@@ -379,11 +381,12 @@ module AnnotateModels
# in subdirectories without namespacing. # in subdirectories without namespacing.
def get_model_class(file) def get_model_class(file)
model_path = file.gsub(/\.rb$/, '') model_path = file.gsub(/\.rb$/, '')
model_dir.each { |dir| model_path = model_path.gsub(/^#{dir}/, '').gsub(/^\//, '') }
begin begin
get_loaded_model(model_path) or raise LoadError.new("cannot load a model from #{file}") get_loaded_model(model_path) or raise LoadError.new("cannot load a model from #{file}")
rescue LoadError rescue LoadError
# this is for non-rails projects, which don't get Rails auto-require magic # this is for non-rails projects, which don't get Rails auto-require magic
if Kernel.require(File.expand_path("#{model_dir}/#{model_path}")) if Kernel.require(file)
retry retry
elsif model_path.match(/\//) elsif model_path.match(/\//)
model_path = model_path.split('/')[1..-1].join('/').to_s model_path = model_path.split('/')[1..-1].join('/').to_s
...@@ -428,10 +431,10 @@ module AnnotateModels ...@@ -428,10 +431,10 @@ module AnnotateModels
annotated = [] annotated = []
get_model_files(options).each do |file| get_model_files(options).each do |file|
annotate_model_file(annotated, file, header, options) annotate_model_file(annotated, File.join(file), header, options)
end end
if annotated.empty? if annotated.empty?
puts "Nothing annotated." puts "Nothing to annotate."
else else
puts "Annotated (#{annotated.length}): #{annotated.join(', ')}" puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
end end
...@@ -456,12 +459,13 @@ module AnnotateModels ...@@ -456,12 +459,13 @@ module AnnotateModels
deannotated = [] deannotated = []
deannotated_klass = false deannotated_klass = false
get_model_files(options).each do |file| get_model_files(options).each do |file|
file = File.join(file)
begin begin
klass = get_model_class(file) klass = get_model_class(file)
if klass < ActiveRecord::Base && !klass.abstract_class? if klass < ActiveRecord::Base && !klass.abstract_class?
model_name = klass.name.underscore model_name = klass.name.underscore
table_name = klass.table_name table_name = klass.table_name
model_file_name = File.join(model_dir, file) model_file_name = file
deannotated_klass = true if(remove_annotation_of_file(model_file_name)) deannotated_klass = true if(remove_annotation_of_file(model_file_name))
(TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS). (TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS).
...@@ -475,7 +479,7 @@ module AnnotateModels ...@@ -475,7 +479,7 @@ module AnnotateModels
end end
deannotated << klass if(deannotated_klass) deannotated << klass if(deannotated_klass)
rescue Exception => e rescue Exception => e
puts "Unable to deannotate #{file}: #{e.message}" puts "Unable to deannotate #{File.join(file)}: #{e.message}"
puts "\t" + e.backtrace.join("\n\t") if options[:trace] puts "\t" + e.backtrace.join("\n\t") if options[:trace]
end end
end end
......
...@@ -18,7 +18,7 @@ task :annotate_models => :environment do ...@@ -18,7 +18,7 @@ task :annotate_models => :environment do
options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position']) options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position'])
options[:show_indexes] = Annotate.true?(ENV['show_indexes']) options[:show_indexes] = Annotate.true?(ENV['show_indexes'])
options[:simple_indexes] = Annotate.true?(ENV['simple_indexes']) options[:simple_indexes] = Annotate.true?(ENV['simple_indexes'])
options[:model_dir] = ENV['model_dir'] options[:model_dir] = ENV['model_dir'] ? ENV['model_dir'].split(',') : []
options[:include_version] = Annotate.true?(ENV['include_version']) options[:include_version] = Annotate.true?(ENV['include_version'])
options[:require] = ENV['require'] ? ENV['require'].split(',') : [] options[:require] = ENV['require'] ? ENV['require'].split(',') : []
options[:exclude_tests] = Annotate.true?(ENV['exclude_tests']) options[:exclude_tests] = Annotate.true?(ENV['exclude_tests'])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment