Commit ffceca9b by Marcos Piccinini

add simple_indexes option

parent 513b837f
---
:major: 2
:minor: 3
:patch: 1
# Generated by jeweler
# DO NOT EDIT THIS FILE
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{annotate} s.name = %q{annotate}
s.version = "2.3.1" s.version = "2.3.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Cuong Tran", "Marcos Piccinini"] s.authors = ["Cuong Tran", "Marcos Piccinini"]
s.date = %q{2009-07-02} s.date = %q{2009-08-21}
s.default_executable = %q{annotate} s.default_executable = %q{annotate}
s.email = %q{x@nofxx.com} s.email = %q{x@nofxx.com}
s.executables = ["annotate"] s.executables = ["annotate"]
...@@ -18,7 +21,7 @@ Gem::Specification.new do |s| ...@@ -18,7 +21,7 @@ Gem::Specification.new do |s|
"History.txt", "History.txt",
"README.rdoc", "README.rdoc",
"Rakefile", "Rakefile",
"VERSION.yml", "VERSION",
"annotate.gemspec", "annotate.gemspec",
"bin/annotate", "bin/annotate",
"lib/annotate.rb", "lib/annotate.rb",
...@@ -35,7 +38,7 @@ Gem::Specification.new do |s| ...@@ -35,7 +38,7 @@ Gem::Specification.new do |s|
s.homepage = %q{http://github.com/nofxx/annotate} s.homepage = %q{http://github.com/nofxx/annotate}
s.rdoc_options = ["--charset=UTF-8"] s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.4} s.rubygems_version = %q{1.3.5}
s.summary = %q{Annotates Rails Models, routes, and others} s.summary = %q{Annotates Rails Models, routes, and others}
s.test_files = [ s.test_files = [
"spec/annotate/annotate_models_spec.rb", "spec/annotate/annotate_models_spec.rb",
......
...@@ -7,31 +7,35 @@ task = :annotate_models ...@@ -7,31 +7,35 @@ task = :annotate_models
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "Usage: annotate [options]" opts.banner = "Usage: annotate [options]"
opts.on('-d', '--delete', opts.on('-d', '--delete',
"Remove annotations from all model files") do "Remove annotations from all model files") do
task = :remove_annotation task = :remove_annotation
end end
opts.on('-p', '--position [before|after]', ['before', 'after'], 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| "Place the annotations at the top (before) or the bottom (after) of the model file") do |p|
ENV['position'] = p ENV['position'] = p
end end
opts.on('-r', '--routes', opts.on('-r', '--routes',
"Annotate routes.rb with the output of 'rake routes'") do "Annotate routes.rb with the output of 'rake routes'") do
task = :annotate_routes task = :annotate_routes
end end
opts.on('-v', '--version', opts.on('-v', '--version',
"Show the current version of this gem") do "Show the current version of this gem") do
puts "Annotate v#{Annotate::VERSION}"; exit puts "Annotate v#{Annotate::VERSION}"; exit
end end
opts.on('-m', '--show-migration', opts.on('-m', '--show-migration',
"Include the migration version number in the annotation") do "Include the migration version number in the annotation") do
ENV['include_version'] = "yes" ENV['include_version'] = "yes"
end end
opts.on('-i', '--show-indexes', opts.on('-i', '--show-indexes',
"List the table's database indexes in the annotation") do "List the table's database indexes in the annotation") do
ENV['show_indexes'] = "yes" ENV['show_indexes'] = "yes"
end end
opts.on('--model-dir dir', opts.on('-s', '--simple-indexes',
"Concat the column's related indexes in the annotation") do
ENV['simple_indexes'] = "yes"
end
opts.on('--model-dir dir',
"Annotate model files stored in dir rather than app/models") do |dir| "Annotate model files stored in dir rather than app/models") do |dir|
ENV['model_dir'] = dir ENV['model_dir'] = dir
end end
......
...@@ -64,6 +64,17 @@ module AnnotateModels ...@@ -64,6 +64,17 @@ module AnnotateModels
attrs << "#{col.geometry_type}, #{col.srid}" attrs << "#{col.geometry_type}, #{col.srid}"
end end
# Check if the column has indices and print "indexed" if true
# If the indice include another colum, print it too.
if options[:simple_indexes] # Check out if this column is indexed
indices = klass.connection.indexes(klass.table_name)
if indices = indices.select { |ind| ind.columns.include? col.name }
indices.each do |ind|
ind = ind.columns.reject! { |i| i == col.name }
attrs << (ind.length == 0 ? "indexed" : "indexed => [#{ind.join(", ")}]")
end
end
end
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n" info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
end end
......
...@@ -5,9 +5,10 @@ task :annotate_models => :environment do ...@@ -5,9 +5,10 @@ task :annotate_models => :environment do
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[:simple_indexes] = ENV['simple_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[:require] = ENV['require'].split(',') options[:require] = ENV['require'].split(',') rescue []
AnnotateModels.do_annotations(options) AnnotateModels.do_annotations(options)
end end
......
...@@ -23,6 +23,7 @@ describe AnnotateModels do ...@@ -23,6 +23,7 @@ describe AnnotateModels do
it "should get schema info" do it "should get schema info" do
AnnotateModels.get_schema_info(mock_klass( AnnotateModels.get_schema_info(mock_klass(
:connection => mock("Conn", :indexes => []),
:table_name => "users", :table_name => "users",
:primary_key => "id", :primary_key => "id",
:column_names => ["id","login"], :column_names => ["id","login"],
...@@ -41,7 +42,7 @@ describe AnnotateModels do ...@@ -41,7 +42,7 @@ describe AnnotateModels do
EOS EOS
end end
describe "#get_model_class" do describe "#get_model_class" do
module ::ActiveRecord module ::ActiveRecord
class Base class Base
...@@ -61,7 +62,7 @@ EOS ...@@ -61,7 +62,7 @@ EOS
AnnotateModels.model_dir = @dir AnnotateModels.model_dir = @dir
create('foo.rb', <<-EOS) create('foo.rb', <<-EOS)
class Foo < ActiveRecord::Base class Foo < ActiveRecord::Base
end end
EOS EOS
create('foo_with_macro.rb', <<-EOS) create('foo_with_macro.rb', <<-EOS)
class FooWithMacro < ActiveRecord::Base class FooWithMacro < ActiveRecord::Base
......
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