Commit 46dc0846 by Cuong Tran

Merge pull request #226 from datemyschool/annotate_multiple_dir

Add posibility to define model_dir as an array of directorie paths
parents c40faf4a b2fbd90a
......@@ -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,8 +111,10 @@ module Annotate
klass.eager_load!
end
else
FileList["#{options[:model_dir]}/**/*.rb"].each do |fname|
require File.expand_path(fname)
options[:model_dir].each do |dir|
FileList["#{dir}/**/*.rb"].each do |fname|
require File.expand_path(fname)
end
end
end
end
......
......@@ -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"]
else
Dir["**/*.rb"].reject{ |f| f["concerns/"] }
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/"] }.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'])
......
......@@ -136,9 +136,8 @@ EOS
# todo: use 'files' gem instead
def create(file, body="hi")
file_path = File.join(AnnotateModels.model_dir, file)
file_path = File.join(AnnotateModels.model_dir[0], file)
FileUtils.mkdir_p(File.dirname(file_path))
File.open(file_path, "wb") do |f|
f.puts(body)
end
......@@ -146,7 +145,7 @@ EOS
end
def check_class_name(file, class_name)
klass = AnnotateModels.get_model_class(file)
klass = AnnotateModels.get_model_class(File.join(AnnotateModels.model_dir[0], file))
expect(klass).not_to eq(nil)
expect(klass.name).to eq(class_name)
......@@ -294,7 +293,7 @@ EOS
CONSTANT = 1
end
EOS
path = File.expand_path("#{AnnotateModels.model_dir}/loaded_class")
path = File.expand_path('loaded_class', AnnotateModels.model_dir[0])
Kernel.load "#{path}.rb"
expect(Kernel).not_to receive(:require).with(path)
......@@ -557,7 +556,7 @@ end
it "displays an error message" do
expect(capturing(:stdout) {
AnnotateModels.do_annotations :model_dir => @model_dir, :is_rake => true
}).to include("Unable to annotate user.rb: oops")
}).to include("Unable to annotate #{@model_dir}/user.rb: oops")
end
it "displays the full stack trace with --trace" do
......@@ -587,7 +586,7 @@ end
it "displays an error message" do
expect(capturing(:stdout) {
AnnotateModels.remove_annotations :model_dir => @model_dir, :is_rake => true
}).to include("Unable to deannotate user.rb: oops")
}).to include("Unable to deannotate #{@model_dir}/user.rb: oops")
end
it "displays the full stack trace" do
......
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