Skip to content

Commit

Permalink
Merge pull request #226 from datemyschool/annotate_multiple_dir
Browse files Browse the repository at this point in the history
Add posibility to define model_dir as an array of directorie paths
  • Loading branch information
ctran committed Jan 15, 2015
2 parents c40faf4 + b2fbd90 commit 46dc084
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.rdoc 100644 → 100755
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bin/annotate
Expand Up @@ -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

Expand Down
12 changes: 7 additions & 5 deletions lib/annotate.rb 100644 → 100755
Expand Up @@ -27,10 +27,10 @@ module Annotate
:timestamp, :exclude_serializers
]
OTHER_OPTIONS=[
:model_dir, :ignore_columns
:ignore_columns
]
PATH_OPTIONS=[
:require,
:require, :model_dir
]


Expand Down Expand Up @@ -70,7 +70,7 @@ def self.setup_options(options = {})
end

if(!options[:model_dir])
options[:model_dir] = 'app/models'
options[:model_dir] = ['app/models']
end

return options
Expand Down Expand Up @@ -111,8 +111,10 @@ def self.eager_load(options)
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
Expand Down
30 changes: 17 additions & 13 deletions lib/annotate/annotate_models.rb 100644 → 100755
Expand Up @@ -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)
Expand Down Expand Up @@ -311,7 +311,7 @@ def annotate(klass, file, header, options={})
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
Expand Down Expand Up @@ -357,15 +357,17 @@ def get_model_files(options)
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
Expand All @@ -379,11 +381,12 @@ def get_model_files(options)
# 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
Expand Down Expand Up @@ -428,10 +431,10 @@ def do_annotations(options={})

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
Expand All @@ -456,12 +459,13 @@ def remove_annotations(options={})
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).
Expand All @@ -475,7 +479,7 @@ def remove_annotations(options={})
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
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/annotate_models.rake
Expand Up @@ -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'])
Expand Down
11 changes: 5 additions & 6 deletions spec/annotate/annotate_models_spec.rb 100644 → 100755
Expand Up @@ -136,17 +136,16 @@ def self.has_many name

# 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
file_path
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)
Expand Down Expand Up @@ -294,7 +293,7 @@ class LoadedClass < ActiveRecord::Base
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)

Expand Down Expand Up @@ -557,7 +556,7 @@ class User < ActiveRecord::Base
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
Expand Down Expand Up @@ -587,7 +586,7 @@ class User < ActiveRecord::Base
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
Expand Down

0 comments on commit 46dc084

Please sign in to comment.