Commit 79353d47 by Marcos Piccinini

Merge commit 'alex/master'

* commit 'alex/master': expand path (to help prevent double-require warnings) cleaned up options specifications
parents 667b76ea 3738206b
......@@ -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
......
......@@ -185,7 +185,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}" # this is for non-rails projects, which don't get Rails auto-require magic
require File.expand_path("#{model_dir}/#{file}") # this is for non-rails projects, which don't get Rails auto-require magic
model = file.gsub(/\.rb$/, '').camelize
parts = model.split('::')
begin
......@@ -198,8 +198,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]
......@@ -227,14 +233,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]
......@@ -262,3 +267,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
......@@ -39,5 +41,42 @@ describe AnnotateModels do
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