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