Commit 28a44896 by Alex Chaffee

changes to support non-Rails projects: make 'model_dir' a parameter; require model files

parent 4aee03d3
...@@ -12,6 +12,7 @@ OptionParser.new do |opts| ...@@ -12,6 +12,7 @@ OptionParser.new do |opts|
opts.on('-r', '--routes') { task = :annotate_routes } opts.on('-r', '--routes') { task = :annotate_routes }
opts.on('-v', '--version') { puts "Annotate v#{Annotate::VERSION}"; exit } opts.on('-v', '--version') { puts "Annotate v#{Annotate::VERSION}"; exit }
opts.on('-m', '--show-migration') { ENV['include_version'] = "yes" } opts.on('-m', '--show-migration') { ENV['include_version'] = "yes" }
opts.on('-l', '--model-dir dir') {|dir| ENV['model_dir'] = dir }
end.parse! end.parse!
begin begin
......
...@@ -4,7 +4,6 @@ module AnnotateModels ...@@ -4,7 +4,6 @@ module AnnotateModels
COMPAT_PREFIX = "== Schema Info" COMPAT_PREFIX = "== Schema Info"
PREFIX = "== Schema Information" PREFIX = "== Schema Information"
MODEL_DIR = "app/models"
FIXTURE_DIRS = ["test/fixtures","spec/fixtures"] FIXTURE_DIRS = ["test/fixtures","spec/fixtures"]
# File.join for windows reverse bar compat? # File.join for windows reverse bar compat?
# I dont use windows, can`t test # I dont use windows, can`t test
...@@ -13,6 +12,13 @@ module AnnotateModels ...@@ -13,6 +12,13 @@ module AnnotateModels
# Object Daddy http://github.com/flogic/object_daddy/tree/master # Object Daddy http://github.com/flogic/object_daddy/tree/master
EXEMPLARS_DIR = File.join("spec", "exemplars") EXEMPLARS_DIR = File.join("spec", "exemplars")
def model_dir
@model_dir || "app/models"
end
def model_dir=(dir)
@model_dir = dir
end
# Simple quoting for the default column value # Simple quoting for the default column value
def quote(value) def quote(value)
...@@ -119,7 +125,7 @@ module AnnotateModels ...@@ -119,7 +125,7 @@ module AnnotateModels
info = get_schema_info(klass, header) info = get_schema_info(klass, header)
annotated = false annotated = false
model_name = klass.name.underscore model_name = klass.name.underscore
model_file_name = File.join(MODEL_DIR, file) model_file_name = File.join(model_dir, file)
if annotate_one_file(model_file_name, info, options.merge( if annotate_one_file(model_file_name, info, options.merge(
:position=>(options[:position_in_class] || options[:position]))) :position=>(options[:position_in_class] || options[:position])))
annotated = true annotated = true
...@@ -142,13 +148,13 @@ module AnnotateModels ...@@ -142,13 +148,13 @@ module AnnotateModels
# command line arguments, they're assumed to be either # command line arguments, they're assumed to be either
# the underscore or CamelCase versions of model names. # the underscore or CamelCase versions of model names.
# Otherwise we take all the model files in the # Otherwise we take all the model files in the
# app/models directory. # model_dir directory.
def get_model_files def get_model_files
models = ARGV.dup models = ARGV.dup
models.shift models.shift
models.reject!{|m| m.starts_with?("position=")} models.reject!{|m| m.starts_with?("position=")}
if models.empty? if models.empty?
Dir.chdir(MODEL_DIR) do Dir.chdir(model_dir) do
models = Dir["**/*.rb"] models = Dir["**/*.rb"]
end end
end end
...@@ -159,6 +165,7 @@ module AnnotateModels ...@@ -159,6 +165,7 @@ module AnnotateModels
# Check for namespaced models in subdirectories as well as models # Check for namespaced models in subdirectories as well as models
# in subdirectories without namespacing. # in subdirectories without namespacing.
def get_model_class(file) def get_model_class(file)
require "#{model_dir}/#{file}"
model = file.gsub(/\.rb$/, '').camelize model = file.gsub(/\.rb$/, '').camelize
parts = model.split('::') parts = model.split('::')
begin begin
...@@ -182,6 +189,10 @@ module AnnotateModels ...@@ -182,6 +189,10 @@ module AnnotateModels
end end
end end
if options[:model_dir]
self.model_dir = options[:model_dir]
end
annotated = [] annotated = []
get_model_files.each do |file| get_model_files.each do |file|
begin begin
...@@ -202,7 +213,10 @@ module AnnotateModels ...@@ -202,7 +213,10 @@ module AnnotateModels
end end
end end
def remove_annotations def remove_annotations(options={})
if options[:model_dir]
self.model_dir = options[:model_dir]
end
deannotated = [] deannotated = []
get_model_files.each do |file| get_model_files.each do |file|
begin begin
...@@ -210,7 +224,7 @@ module AnnotateModels ...@@ -210,7 +224,7 @@ module AnnotateModels
if klass < ActiveRecord::Base && !klass.abstract_class? if klass < ActiveRecord::Base && !klass.abstract_class?
deannotated << klass deannotated << klass
model_file_name = File.join(MODEL_DIR, file) model_file_name = File.join(model_dir, file)
remove_annotation_of_file(model_file_name) remove_annotation_of_file(model_file_name)
FIXTURE_DIRS.each do |dir| FIXTURE_DIRS.each do |dir|
......
...@@ -5,11 +5,14 @@ task :annotate_models => :environment do ...@@ -5,11 +5,14 @@ task :annotate_models => :environment do
options[:position_in_class] = ENV['position_in_class'] || ENV['position'] options[:position_in_class] = ENV['position_in_class'] || ENV['position']
options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position'] options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position']
options[:include_version] = ENV['include_version'] options[:include_version] = ENV['include_version']
options[:model_dir] = ENV['model_dir']
AnnotateModels.do_annotations(options) AnnotateModels.do_annotations(options)
end end
desc "Remove schema information from model and fixture files" desc "Remove schema information from model and fixture files"
task :remove_annotation => :environment do task :remove_annotation => :environment do
require 'annotate_models' require 'annotate_models'
AnnotateModels.remove_annotations options={}
options[:model_dir] = ENV['model_dir']
AnnotateModels.remove_annotations(options)
end end
\ No newline at end of file
...@@ -4,6 +4,7 @@ task :annotate_models => :environment do ...@@ -4,6 +4,7 @@ task :annotate_models => :environment do
options={} options={}
options[:position_in_class] = ENV['position_in_class'] || ENV['position'] || :before options[:position_in_class] = ENV['position_in_class'] || ENV['position'] || :before
options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position'] || :before options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position'] || :before
options[:model_dir] = ENV['model_dir']
AnnotateModels.do_annotations(options) AnnotateModels.do_annotations(options)
end end
......
require 'test/unit' require 'test/unit'
require File.dirname(__FILE__) + '/../lib/annotate_models' require File.dirname(__FILE__) + '/../lib/annotate'
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