Skip to content

Commit

Permalink
add simple_indexes option
Browse files Browse the repository at this point in the history
  • Loading branch information
nofxx committed Aug 21, 2009
1 parent 513b837 commit ffceca9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 22 deletions.
1 change: 1 addition & 0 deletions VERSION
@@ -0,0 +1 @@
2.3.2
4 changes: 0 additions & 4 deletions VERSION.yml

This file was deleted.

11 changes: 7 additions & 4 deletions annotate.gemspec
@@ -1,12 +1,15 @@
# 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"]
Expand All @@ -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",
Expand All @@ -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",
Expand Down
26 changes: 15 additions & 11 deletions bin/annotate
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/annotate/annotate_models.rb
Expand Up @@ -64,6 +64,17 @@ def get_schema_info(klass, header, options = {})
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


Expand Down
3 changes: 2 additions & 1 deletion lib/tasks/annotate_models.rake
Expand Up @@ -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


Expand Down
5 changes: 3 additions & 2 deletions spec/annotate/annotate_models_spec.rb
Expand Up @@ -23,6 +23,7 @@ def mock_column(stubs={})
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"],
Expand All @@ -41,7 +42,7 @@ def mock_column(stubs={})
EOS EOS


end end

describe "#get_model_class" do describe "#get_model_class" do
module ::ActiveRecord module ::ActiveRecord
class Base class Base
Expand All @@ -61,7 +62,7 @@ def create(file, body="hi")
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
Expand Down

0 comments on commit ffceca9

Please sign in to comment.