Commit 8d3c00c7 by Tony Day

Changed to sort be default with an option to not sort.

parent e0527559
...@@ -4,3 +4,4 @@ rdoc/* ...@@ -4,3 +4,4 @@ rdoc/*
coverage/* coverage/*
spec/debug.log spec/debug.log
pkg/* pkg/*
.idea
...@@ -13,7 +13,7 @@ OptionParser.new do |opts| ...@@ -13,7 +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('-n', '--no-sort', "Sort columns alphabetically (rather than listing in creation order)") { ENV['no_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!
......
...@@ -3,7 +3,7 @@ module AnnotateModels ...@@ -3,7 +3,7 @@ module AnnotateModels
# Annotate Models plugin use this header # Annotate Models plugin use this header
COMPAT_PREFIX = "== Schema Info" COMPAT_PREFIX = "== Schema Info"
PREFIX = "== Schema Information" PREFIX = "== Schema Information"
FIXTURE_DIRS = ["test/fixtures","spec/fixtures"] FIXTURE_DIRS = ["test/fixtures","spec/fixtures"]
# File.join for windows reverse bar compat? # File.join for windows reverse bar compat?
# I dont use windows, can`t test # I dont use windows, can`t test
...@@ -11,11 +11,11 @@ module AnnotateModels ...@@ -11,11 +11,11 @@ module AnnotateModels
SPEC_MODEL_DIR = File.join("spec", "models") SPEC_MODEL_DIR = File.join("spec", "models")
# Object Daddy http://github.com/flogic/object_daddy/tree/master # Object Daddy http://github.com/flogic/object_daddy/tree/master
EXEMPLARS_DIR = File.join("spec", "exemplars") EXEMPLARS_DIR = File.join("spec", "exemplars")
def model_dir def model_dir
@model_dir || "app/models" @model_dir || "app/models"
end end
def model_dir=(dir) def model_dir=(dir)
@model_dir = dir @model_dir = dir
end end
...@@ -56,16 +56,16 @@ module AnnotateModels ...@@ -56,16 +56,16 @@ module AnnotateModels
else else
col_type << "(#{col.limit})" if col.limit col_type << "(#{col.limit})" if col.limit
end end
# Check out if we got a geometric column # Check out if we got a geometric column
# and print the type and SRID # and print the type and SRID
if col.respond_to?(:geometry_type) if col.respond_to?(:geometry_type)
attrs << "#{col.geometry_type}, #{col.srid}" attrs << "#{col.geometry_type}, #{col.srid}"
end end
cols << 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] cols.sort! unless options[:no_sort]
info << cols.join info << cols.join
if options[:show_indexes] if options[:show_indexes]
...@@ -95,7 +95,7 @@ module AnnotateModels ...@@ -95,7 +95,7 @@ module AnnotateModels
# Returns true or false depending on whether the file was modified. # Returns true or false depending on whether the file was modified.
# #
# === Options (opts) # === Options (opts)
# :position<Symbol>:: where to place the annotated section in fixture or model file, # :position<Symbol>:: where to place the annotated section in fixture or model file,
# "before" or "after". Default is "before". # "before" or "after". Default is "before".
# :position_in_class<Symbol>:: where to place the annotated section in model file # :position_in_class<Symbol>:: where to place the annotated section in model file
# :position_in_fixture<Symbol>:: where to place the annotated section in fixture file # :position_in_fixture<Symbol>:: where to place the annotated section in fixture file
...@@ -108,7 +108,7 @@ module AnnotateModels ...@@ -108,7 +108,7 @@ module AnnotateModels
header = Regexp.new(/(^# Table name:.*?\n(#.*\n)*\n)/) header = Regexp.new(/(^# Table name:.*?\n(#.*\n)*\n)/)
old_header = old_content.match(header).to_s old_header = old_content.match(header).to_s
new_header = info_block.match(header).to_s new_header = info_block.match(header).to_s
if old_header == new_header if old_header == new_header
false false
else else
...@@ -123,13 +123,13 @@ module AnnotateModels ...@@ -123,13 +123,13 @@ module AnnotateModels
end end
end end
end end
def remove_annotation_of_file(file_name) def remove_annotation_of_file(file_name)
if File.exist?(file_name) if File.exist?(file_name)
content = File.read(file_name) content = File.read(file_name)
content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '') content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
File.open(file_name, "wb") { |f| f.puts content } File.open(file_name, "wb") { |f| f.puts content }
end end
end end
...@@ -154,7 +154,7 @@ module AnnotateModels ...@@ -154,7 +154,7 @@ module AnnotateModels
[ [
File.join(UNIT_TEST_DIR, "#{model_name}_test.rb"), # test File.join(UNIT_TEST_DIR, "#{model_name}_test.rb"), # test
File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec
File.join(EXEMPLARS_DIR, "#{model_name}_exemplar.rb"), # Object Daddy File.join(EXEMPLARS_DIR, "#{model_name}_exemplar.rb"), # Object Daddy
].each { |file| annotate_one_file(file, info) } ].each { |file| annotate_one_file(file, info) }
FIXTURE_DIRS.each do |dir| FIXTURE_DIRS.each do |dir|
...@@ -180,7 +180,7 @@ module AnnotateModels ...@@ -180,7 +180,7 @@ module AnnotateModels
end end
models models
end end
# Retrieve the classes belonging to the model names we're asked to process # Retrieve the classes belonging to the model names we're asked to process
# Check for namespaced models in subdirectories as well as models # Check for namespaced models in subdirectories as well as models
# in subdirectories without namespacing. # in subdirectories without namespacing.
...@@ -206,9 +206,9 @@ module AnnotateModels ...@@ -206,9 +206,9 @@ module AnnotateModels
version = ActiveRecord::Migrator.current_version rescue 0 version = ActiveRecord::Migrator.current_version rescue 0
if version > 0 if version > 0
header << "\n# Schema version: #{version}" header << "\n# Schema version: #{version}"
end end
end end
if options[:model_dir] if options[:model_dir]
self.model_dir = options[:model_dir] self.model_dir = options[:model_dir]
end end
...@@ -232,7 +232,7 @@ module AnnotateModels ...@@ -232,7 +232,7 @@ module AnnotateModels
puts "Annotated (#{annotated.length}): #{annotated.join(', ')}" puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
end end
end end
def remove_annotations(options={}) def remove_annotations(options={})
p options p options
if options[:model_dir] if options[:model_dir]
...@@ -245,10 +245,10 @@ module AnnotateModels ...@@ -245,10 +245,10 @@ module AnnotateModels
klass = get_model_class(file) klass = get_model_class(file)
if klass < ActiveRecord::Base && !klass.abstract_class? if klass < ActiveRecord::Base && !klass.abstract_class?
deannotated << klass 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) remove_annotation_of_file(model_file_name)
FIXTURE_DIRS.each do |dir| FIXTURE_DIRS.each do |dir|
fixture_file_name = File.join(dir,klass.table_name + ".yml") fixture_file_name = File.join(dir,klass.table_name + ".yml")
remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name) remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name)
......
...@@ -4,10 +4,10 @@ task :annotate_models => :environment do ...@@ -4,10 +4,10 @@ task :annotate_models => :environment do
options={} options={}
options[:position_in_class] = ENV['position_in_class'] || ENV['position'] || :before options[:position_in_class] = ENV['position_in_class'] || ENV['position'] || :before
options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position'] || :before options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position'] || :before
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'] options[:no_sort] = ENV['no_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