Commit aba79ef7 by Alex Chaffee

cleaned up options specifications

added '-R' option to require additional files ignore unknown macros ('acts_as_whatever') (with specs!)
parent 8ecd23ee
......@@ -7,13 +7,42 @@ task = :annotate_models
OptionParser.new do |opts|
opts.banner = "Usage: annotate [options]"
opts.on('-d', '--delete', "Remove annotations from all model files") { task = :remove_annotation }
opts.on('-p', '--position [before|after]', ['before', 'after'], "Place the annotations at the top (before) or the bottom (after) of the model file") { |p| ENV['position'] = p }
opts.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") { task = :annotate_routes }
opts.on('-v', '--version', "Show the current version of this gem") { puts "Annotate v#{Annotate::VERSION}"; exit }
opts.on('-m', '--show-migration', "Include the migration version number in the annotation") { ENV['include_version'] = "yes" }
opts.on('-i', '--show-indexes', "List the table's database indexes in the annotation") { ENV['show_indexes'] = "yes" }
opts.on('--model-dir dir', "Annotate model files stored in dir rather than app/models") {|dir| ENV['model_dir'] = dir }
opts.on('-d', '--delete',
"Remove annotations from all model files") do
task = :remove_annotation
end
opts.on('-p', '--position [before|after]', ['before', 'after'],
"Place the annotations at the top (before) or the bottom (after) of the model file") do |p|
ENV['position'] = p
end
opts.on('-r', '--routes',
"Annotate routes.rb with the output of 'rake routes'") do
task = :annotate_routes
end
opts.on('-v', '--version',
"Show the current version of this gem") do
puts "Annotate v#{Annotate::VERSION}"; exit
end
opts.on('-m', '--show-migration',
"Include the migration version number in the annotation") do
ENV['include_version'] = "yes"
end
opts.on('-i', '--show-indexes',
"List the table's database indexes in the annotation") do
ENV['show_indexes'] = "yes"
end
opts.on('--model-dir dir',
"Annotate model files stored in dir rather than app/models") do |dir|
ENV['model_dir'] = dir
end
opts.on('-R', '--require path',
"Additional files to require before loading models") do |path|
if ENV['require']
ENV['require'] = ENV['require'] + ",#{path}"
else
ENV['require'] = path
end
end
end.parse!
if Annotate.load_tasks
......
......@@ -195,8 +195,14 @@ module AnnotateModels
# We're passed a name of things that might be
# ActiveRecord models. If we can find the class, and
# if its a subclass of ActiveRecord::Base,
# then pas it to the associated block
# then pass it to the associated block
def do_annotations(options={})
if options[:require]
options[:require].each do |path|
require path
end
end
header = PREFIX.dup
if options[:include_version]
......@@ -224,14 +230,13 @@ module AnnotateModels
end
end
if annotated.empty?
puts "Nothing annotated!"
puts "Nothing annotated."
else
puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
end
end
def remove_annotations(options={})
p options
if options[:model_dir]
puts "removing"
self.model_dir = options[:model_dir]
......@@ -259,3 +264,13 @@ module AnnotateModels
end
end
end
# monkey patches
module ::ActiveRecord
class Base
def self.method_missing(name, *args)
# ignore this, so unknown/unloaded macros won't cause parsing to fail
end
end
end
......@@ -7,6 +7,7 @@ task :annotate_models => :environment do
options[:show_indexes] = ENV['show_indexes']
options[:model_dir] = ENV['model_dir']
options[:include_version] = ENV['include_version']
options[:require] = ENV['require'].split(',')
AnnotateModels.do_annotations(options)
end
......
require File.dirname(__FILE__) + '/../spec_helper.rb'
require 'annotate/annotate_models'
require 'rubygems'
require 'activesupport'
describe AnnotateModels do
......@@ -40,4 +42,41 @@ EOS
end
describe "#get_model_class" do
module ::ActiveRecord
class Base
end
end
def create(file, body="hi")
File.open(@dir + '/' + file, "w") do |f|
f.puts(body)
end
end
before :all do
require "tmpdir"
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/annotate_models"
FileUtils.mkdir_p(@dir)
AnnotateModels.model_dir = @dir
create('foo.rb', <<-EOS)
class Foo < ActiveRecord::Base
end
EOS
create('foo_with_macro.rb', <<-EOS)
class FooWithMacro < ActiveRecord::Base
acts_as_awesome :yah
end
EOS
end
it "should work" do
klass = AnnotateModels.get_model_class("foo.rb")
klass.name.should == "Foo"
end
it "should not care about unknown macros" do
klass = AnnotateModels.get_model_class("foo_with_macro.rb")
klass.name.should == "FooWithMacro"
end
end
end
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