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
-m, --show-migration Include the migration version number 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
--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
--sort Sort columns alphabetically, rather than in creation order
-R, --require path Additional file to require before loading models, may be used multiple times
......
......@@ -120,7 +120,7 @@ OptionParser.new do |opts|
end
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
end
......
......@@ -27,10 +27,10 @@ module Annotate
:timestamp, :exclude_serializers
]
OTHER_OPTIONS=[
:model_dir, :ignore_columns
:ignore_columns
]
PATH_OPTIONS=[
:require,
:require, :model_dir
]
......@@ -70,7 +70,7 @@ module Annotate
end
if(!options[:model_dir])
options[:model_dir] = 'app/models'
options[:model_dir] = ['app/models']
end
return options
......@@ -111,11 +111,13 @@ module Annotate
klass.eager_load!
end
else
FileList["#{options[:model_dir]}/**/*.rb"].each do |fname|
options[:model_dir].each do |dir|
FileList["#{dir}/**/*.rb"].each do |fname|
require File.expand_path(fname)
end
end
end
end
def self.bootstrap_rake
begin
......
......@@ -73,7 +73,7 @@ module AnnotateModels
class << self
def model_dir
@model_dir || "app/models"
@model_dir.is_a?(Array) ? @model_dir : [@model_dir || "app/models"]
end
def model_dir=(dir)
......@@ -311,7 +311,7 @@ module AnnotateModels
did_annotate = false
model_name = klass.name.underscore
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))
did_annotate = true
......@@ -357,15 +357,17 @@ module AnnotateModels
models.reject!{|m| m.match(/^(.*)=/)}
if models.empty?
begin
Dir.chdir(model_dir) do
models = if options[:ignore_model_sub_dir]
Dir["*.rb"]
model_dir.each do |dir|
Dir.chdir(dir) do
models.concat( if options[:ignore_model_sub_dir]
Dir["*.rb"].map{ |f| [dir, f] }
else
Dir["**/*.rb"].reject{ |f| f["concerns/"] }
Dir["**/*.rb"].reject{ |f| f["concerns/"] }.map{ |f| [dir, f] }
end)
end
end
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 "Call 'annotate --help' for more info."
exit 1
......@@ -379,11 +381,12 @@ module AnnotateModels
# in subdirectories without namespacing.
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}")
rescue LoadError
# 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
elsif model_path.match(/\//)
model_path = model_path.split('/')[1..-1].join('/').to_s
......@@ -428,10 +431,10 @@ module AnnotateModels
annotated = []
get_model_files(options).each do |file|
annotate_model_file(annotated, file, header, options)
annotate_model_file(annotated, File.join(file), header, options)
end
if annotated.empty?
puts "Nothing annotated."
puts "Nothing to annotate."
else
puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
end
......@@ -456,12 +459,13 @@ module AnnotateModels
deannotated = []
deannotated_klass = false
get_model_files(options).each do |file|
file = File.join(file)
begin
klass = get_model_class(file)
if klass < ActiveRecord::Base && !klass.abstract_class?
model_name = klass.name.underscore
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))
(TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS).
......@@ -475,7 +479,7 @@ module AnnotateModels
end
deannotated << klass if(deannotated_klass)
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]
end
end
......
......@@ -18,7 +18,7 @@ task :annotate_models => :environment do
options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position'])
options[:show_indexes] = Annotate.true?(ENV['show_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[:require] = ENV['require'] ? ENV['require'].split(',') : []
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