Commit 6aee8813 by Alex Chaffee

Merge git://github.com/bpot/annotate_models into merging-bpot

* git://github.com/bpot/annotate_models: Add options for show indexes in annotations
parents 928e997f baa00178
......@@ -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('-i', '--show-indexes') { ENV['show_indexes'] = "yes" }
opts.on('--model-dir dir') {|dir| ENV['model_dir'] = dir }
end.parse!
......
......@@ -38,7 +38,7 @@ module AnnotateModels
# to create a comment block containing a line for
# each column. The line contains the column name,
# the type (and length), and any optional attributes
def get_schema_info(klass, header)
def get_schema_info(klass, header, options = {})
info = "# #{header}\n#\n"
info << "# Table name: #{klass.table_name}\n#\n"
......@@ -65,9 +65,26 @@ module AnnotateModels
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
end
if options[:show_indexes]
info << get_index_info(klass)
end
info << "#\n\n"
end
def get_index_info(klass)
index_info = "#\n# Indexes\n#\n"
indexes = klass.connection.indexes(klass.table_name)
return "" if indexes.empty?
max_size = indexes.collect{|index| index.name.size}.max + 1
indexes.each do |index|
index_info << sprintf("# %-#{max_size}.#{max_size}s %s %s", index.name, "(#{index.columns.join(",")})", index.unique ? "UNIQUE" : "").rstrip + "\n"
end
return index_info
end
# Add a schema block to a file. If the file already contains
# a schema info block (a comment starting with "== Schema Information"), check if it
# matches the block that is already there. If so, leave it be. If not, remove the old
......@@ -122,7 +139,7 @@ module AnnotateModels
# files were modified.
def annotate(klass, file, header,options={})
info = get_schema_info(klass, header)
info = get_schema_info(klass, header, options)
annotated = false
model_name = klass.name.underscore
model_file_name = File.join(model_dir, file)
......
desc "Add schema information (as comments) to model and fixture files"
task :annotate_models => :environment do
require 'annotate_models'
options={}
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']
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
end
......@@ -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[:show_indexes] = ENV['show_indexes']
options[:model_dir] = ENV['model_dir']
AnnotateModels.do_annotations(options)
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