Commit 96e2cb57 by Tony Day

Include option to sort columns alphabetically.

parent 8ecd23ee
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f } %w[rubygems rake rake/clean fileutils newgem rubigen hoe].each { |f| require f }
require File.dirname(__FILE__) + '/lib/annotate' require File.dirname(__FILE__) + '/lib/annotate'
# Generate all the Rake tasks # Generate all the Rake tasks
......
...@@ -13,6 +13,7 @@ OptionParser.new do |opts| ...@@ -13,6 +13,7 @@ OptionParser.new do |opts|
opts.on('-v', '--version', "Show the current version of this gem") { puts "Annotate v#{Annotate::VERSION}"; exit } 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('-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('-i', '--show-indexes', "List the table's database indexes in the annotation") { ENV['show_indexes'] = "yes" }
opts.on('-s', '--sort', "Sort columns alphabetically (rather than listing in creation order)") { ENV['sort'] = "yes" }
opts.on('--model-dir dir', "Annotate model files stored in dir rather than app/models") {|dir| ENV['model_dir'] = dir } opts.on('--model-dir dir', "Annotate model files stored in dir rather than app/models") {|dir| ENV['model_dir'] = dir }
end.parse! end.parse!
......
...@@ -43,6 +43,7 @@ module AnnotateModels ...@@ -43,6 +43,7 @@ module AnnotateModels
info << "# Table name: #{klass.table_name}\n#\n" info << "# Table name: #{klass.table_name}\n#\n"
max_size = klass.column_names.collect{|name| name.size}.max + 1 max_size = klass.column_names.collect{|name| name.size}.max + 1
cols = []
klass.columns.each do |col| klass.columns.each do |col|
attrs = [] attrs = []
attrs << "default(#{quote(col.default)})" if col.default attrs << "default(#{quote(col.default)})" if col.default
...@@ -62,8 +63,10 @@ module AnnotateModels ...@@ -62,8 +63,10 @@ module AnnotateModels
attrs << "#{col.geometry_type}, #{col.srid}" attrs << "#{col.geometry_type}, #{col.srid}"
end end
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n" cols << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
end end
cols.sort! if options[:sort]
info << cols.join
if options[:show_indexes] if options[:show_indexes]
info << get_index_info(klass) info << get_index_info(klass)
......
...@@ -7,6 +7,7 @@ task :annotate_models => :environment do ...@@ -7,6 +7,7 @@ task :annotate_models => :environment do
options[:show_indexes] = ENV['show_indexes'] options[:show_indexes] = ENV['show_indexes']
options[:model_dir] = ENV['model_dir'] options[:model_dir] = ENV['model_dir']
options[:include_version] = ENV['include_version'] options[:include_version] = ENV['include_version']
options[:sort] = ENV['sort']
AnnotateModels.do_annotations(options) AnnotateModels.do_annotations(options)
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