tonyday-annotate 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +28 -0
- data/README.rdoc +94 -0
- data/Rakefile +28 -0
- data/annotate_models.gemspec +40 -0
- data/bin/annotate +24 -0
- data/lib/annotate.rb +16 -0
- data/lib/annotate/annotate_models.rb +264 -0
- data/lib/annotate/annotate_routes.rb +41 -0
- data/lib/tasks/annotate_models.rake +20 -0
- data/lib/tasks/annotate_routes.rake +5 -0
- metadata +84 -0
data/History.txt
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
== 2.0 2009-02-03
|
2
|
+
|
3
|
+
* Add annotate_models plugin fork additions
|
4
|
+
* Annotates Rspec and Test Unit models
|
5
|
+
* Annotates Object Daddy exemplars
|
6
|
+
* Annotates geometrical columns
|
7
|
+
* Add AnnotateRoutes rake task
|
8
|
+
* Up gem structure to newgem defaults
|
9
|
+
|
10
|
+
== 1.0.4 2008-09-04
|
11
|
+
|
12
|
+
* Only update modified models since last run, thanks to sant0sk1
|
13
|
+
|
14
|
+
== 1.0.3 2008-05-02
|
15
|
+
|
16
|
+
* Add misc changes from Dustin Sallings and Henrik N
|
17
|
+
* Remove trailing whitespace
|
18
|
+
* More intuitive info messages
|
19
|
+
* Update README file with update-to-date example
|
20
|
+
|
21
|
+
== 1.0.2 2008-03-22
|
22
|
+
|
23
|
+
* Add contributions from Michael Bumann (http://github.com/bumi)
|
24
|
+
* added an option "position" to choose to put the annotation,
|
25
|
+
* spec/fixtures now also get annotated
|
26
|
+
* added a task to remove the annotations
|
27
|
+
* these options can be specified from command line as -d and -p [before|after]
|
28
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
== AnnotateModels
|
2
|
+
|
3
|
+
Add a comment summarizing the current schema to the bottom of each
|
4
|
+
ActiveRecord model, fixture file.
|
5
|
+
|
6
|
+
If you are using Object Daddy, it`ll annotate your example files too.
|
7
|
+
|
8
|
+
# == Schema Info
|
9
|
+
#
|
10
|
+
# Table name: line_items
|
11
|
+
#
|
12
|
+
# id :integer(11) not null, primary key
|
13
|
+
# quantity :integer(11) not null
|
14
|
+
# product_id :integer(11) not null
|
15
|
+
# unit_price :float
|
16
|
+
# order_id :integer(11)
|
17
|
+
#
|
18
|
+
|
19
|
+
class LineItem < ActiveRecord::Base
|
20
|
+
belongs_to :product
|
21
|
+
. . .
|
22
|
+
|
23
|
+
Annotates geometrical columns, geom type and srid, when using SpatialAdapter or PostgisAdapter:
|
24
|
+
|
25
|
+
# == Schema Info
|
26
|
+
#
|
27
|
+
# Table name: trips
|
28
|
+
#
|
29
|
+
# local :geometry point, 4326
|
30
|
+
# path :geometry line_string, 4326
|
31
|
+
|
32
|
+
== Warning
|
33
|
+
|
34
|
+
Note that this code will blow away the initial/final comment
|
35
|
+
block in your models if it looks like it was previously added
|
36
|
+
by annotate models, so you don't want to add additional text
|
37
|
+
to an automatically created comment block.
|
38
|
+
|
39
|
+
* * Back up your model files before using... * *
|
40
|
+
|
41
|
+
== Install
|
42
|
+
|
43
|
+
From rubyforge:
|
44
|
+
|
45
|
+
sudo gem install annotate
|
46
|
+
|
47
|
+
From github:
|
48
|
+
|
49
|
+
gem sources -a http://gems.github.com
|
50
|
+
sudo gem install ctran-annotate
|
51
|
+
|
52
|
+
== Usage
|
53
|
+
|
54
|
+
To annotate all your models:
|
55
|
+
|
56
|
+
cd /path/to/app
|
57
|
+
annotate
|
58
|
+
|
59
|
+
To annotate routes.rb:
|
60
|
+
|
61
|
+
annotate -r
|
62
|
+
|
63
|
+
More options:
|
64
|
+
|
65
|
+
Usage: annotate [options]
|
66
|
+
-d, --delete Remove annotations from all model files
|
67
|
+
-p, --position [before|after] Place the annotations at the top (before) or the bottom (after) of the model file
|
68
|
+
-r, --routes Annotate routes.rb with the output of 'rake routes'
|
69
|
+
-v, --version Show the current version of this gem
|
70
|
+
-m, --show-migration Include the migration version number in the annotation
|
71
|
+
-i, --show-indexes List the table's database indexes in the annotation
|
72
|
+
--model-dir dir Annotate model files stored in dir rather than app/models
|
73
|
+
|
74
|
+
== LICENSE:
|
75
|
+
|
76
|
+
Released under the same license as Ruby. No Support. No Warranty.
|
77
|
+
|
78
|
+
== Author:
|
79
|
+
|
80
|
+
Original code by:
|
81
|
+
|
82
|
+
Dave Thomas -- Pragmatic Programmers, LLC
|
83
|
+
|
84
|
+
Modifications by:
|
85
|
+
|
86
|
+
- Alex Chaffee - http://github.com/alexch - alex@pivotallabs.com
|
87
|
+
- Cuong Tran - http://github.com/ctran
|
88
|
+
- Jack Danger - http://github.com/JackDanger
|
89
|
+
- Michael Bumann - http://github.com/bumi
|
90
|
+
- Henrik Nyh - http://github.com/henrik
|
91
|
+
- Marcos Piccinini - http://github.com/nofxx
|
92
|
+
|
93
|
+
and many others that I may have missed to add.
|
94
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
%w[rubygems rake rake/clean fileutils newgem rubigen hoe].each { |f| require f }
|
2
|
+
require File.dirname(__FILE__) + '/lib/annotate'
|
3
|
+
|
4
|
+
# Generate all the Rake tasks
|
5
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
6
|
+
$hoe = Hoe.new('annotate', Annotate::VERSION) do |p|
|
7
|
+
p.developer('Cuong Tran', 'ctran@pragmaquest.com')
|
8
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
9
|
+
p.rubyforge_name = 'annotate-models'
|
10
|
+
p.url = "http://github.com/ctran/annotate_models"
|
11
|
+
p.summary = "Annotates Rails Models, routes, and others"
|
12
|
+
p.description = "Annotates Rails Models, routes, and others"
|
13
|
+
|
14
|
+
p.extra_dev_deps = [
|
15
|
+
['newgem', ">= #{::Newgem::VERSION}"]
|
16
|
+
]
|
17
|
+
|
18
|
+
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
19
|
+
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
20
|
+
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
21
|
+
p.rsync_args = '-av --delete --ignore-errors'
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
25
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
26
|
+
|
27
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
28
|
+
# task :default => [:spec, :features]
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{annotate}
|
5
|
+
s.version = "2.0.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Tony Day"]
|
9
|
+
s.date = %q{2009-07-23}
|
10
|
+
s.default_executable = %q{annotate}
|
11
|
+
s.description = %q{Annotates Rails Models, routes, and others}
|
12
|
+
s.email = ["tonyd@panztell.com"]
|
13
|
+
s.executables = ["annotate"]
|
14
|
+
s.extra_rdoc_files = ["History.txt", "README.rdoc"]
|
15
|
+
s.files = ["History.txt", "README.rdoc", "Rakefile", "annotate_models.gemspec", "bin/annotate", "lib/annotate.rb", "lib/annotate/annotate_models.rb", "lib/annotate/annotate_routes.rb", "lib/tasks/annotate_models.rake", "lib/tasks/annotate_routes.rake"]
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.homepage = %q{http://github.com/tonyday/annotate_models}
|
18
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.rubyforge_project = %q{annotate-models}
|
21
|
+
s.rubygems_version = %q{1.3.1}
|
22
|
+
s.summary = %q{Annotates Rails Models, routes, and others}
|
23
|
+
|
24
|
+
if s.respond_to? :specification_version then
|
25
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
26
|
+
s.specification_version = 2
|
27
|
+
|
28
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
29
|
+
s.add_development_dependency(%q<newgem>, [">= 1.4.1"])
|
30
|
+
s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<newgem>, [">= 1.4.1"])
|
33
|
+
s.add_dependency(%q<hoe>, [">= 1.8.0"])
|
34
|
+
end
|
35
|
+
else
|
36
|
+
s.add_dependency(%q<newgem>, [">= 1.4.1"])
|
37
|
+
s.add_dependency(%q<hoe>, [">= 1.8.0"])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
data/bin/annotate
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'annotate'
|
5
|
+
|
6
|
+
task = :annotate_models
|
7
|
+
|
8
|
+
OptionParser.new do |opts|
|
9
|
+
opts.banner = "Usage: annotate [options]"
|
10
|
+
opts.on('-d', '--delete', "Remove annotations from all model files") { task = :remove_annotation }
|
11
|
+
opts.on('-p', '--position [before|after]', ['before', 'after'], "Place the annotations at the top (before) or the bottom (after) of the model file") { |p| ENV['position'] = p }
|
12
|
+
opts.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") { task = :annotate_routes }
|
13
|
+
opts.on('-v', '--version', "Show the current version of this gem") { puts "Annotate v#{Annotate::VERSION}"; exit }
|
14
|
+
opts.on('-m', '--show-migration', "Include the migration version number in the annotation") { ENV['include_version'] = "yes" }
|
15
|
+
opts.on('-i', '--show-indexes', "List the table's database indexes in the annotation") { ENV['show_indexes'] = "yes" }
|
16
|
+
opts.on('-s', '--sort', "Sort columns alphabetically (rather than listing in creation order)") { ENV['sort'] = "yes" }
|
17
|
+
opts.on('--model-dir dir', "Annotate model files stored in dir rather than app/models") {|dir| ENV['model_dir'] = dir }
|
18
|
+
end.parse!
|
19
|
+
|
20
|
+
if Annotate.load_tasks
|
21
|
+
Rake::Task[task].invoke
|
22
|
+
else
|
23
|
+
STDERR.puts "Can't find Rakefile. Are we in a Rails folder?"
|
24
|
+
end
|
data/lib/annotate.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
module Annotate
|
5
|
+
VERSION = '2.0.2'
|
6
|
+
|
7
|
+
def self.load_tasks
|
8
|
+
if File.exists?('Rakefile')
|
9
|
+
load 'Rakefile'
|
10
|
+
Dir[File.join(File.dirname(__FILE__), 'tasks', '**/*.rake')].each { |rake| load rake }
|
11
|
+
return true
|
12
|
+
else
|
13
|
+
return false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,264 @@
|
|
1
|
+
module AnnotateModels
|
2
|
+
class << self
|
3
|
+
# Annotate Models plugin use this header
|
4
|
+
COMPAT_PREFIX = "== Schema Info"
|
5
|
+
PREFIX = "== Schema Information"
|
6
|
+
|
7
|
+
FIXTURE_DIRS = ["test/fixtures","spec/fixtures"]
|
8
|
+
# File.join for windows reverse bar compat?
|
9
|
+
# I dont use windows, can`t test
|
10
|
+
UNIT_TEST_DIR = File.join("test", "unit" )
|
11
|
+
SPEC_MODEL_DIR = File.join("spec", "models")
|
12
|
+
# Object Daddy http://github.com/flogic/object_daddy/tree/master
|
13
|
+
EXEMPLARS_DIR = File.join("spec", "exemplars")
|
14
|
+
|
15
|
+
def model_dir
|
16
|
+
@model_dir || "app/models"
|
17
|
+
end
|
18
|
+
|
19
|
+
def model_dir=(dir)
|
20
|
+
@model_dir = dir
|
21
|
+
end
|
22
|
+
|
23
|
+
# Simple quoting for the default column value
|
24
|
+
def quote(value)
|
25
|
+
case value
|
26
|
+
when NilClass then "NULL"
|
27
|
+
when TrueClass then "TRUE"
|
28
|
+
when FalseClass then "FALSE"
|
29
|
+
when Float, Fixnum, Bignum then value.to_s
|
30
|
+
# BigDecimals need to be output in a non-normalized form and quoted.
|
31
|
+
when BigDecimal then value.to_s('F')
|
32
|
+
else
|
33
|
+
value.inspect
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Use the column information in an ActiveRecord class
|
38
|
+
# to create a comment block containing a line for
|
39
|
+
# each column. The line contains the column name,
|
40
|
+
# the type (and length), and any optional attributes
|
41
|
+
def get_schema_info(klass, header, options = {})
|
42
|
+
info = "# #{header}\n#\n"
|
43
|
+
info << "# Table name: #{klass.table_name}\n#\n"
|
44
|
+
|
45
|
+
max_size = klass.column_names.collect{|name| name.size}.max + 1
|
46
|
+
cols = []
|
47
|
+
klass.columns.each do |col|
|
48
|
+
attrs = []
|
49
|
+
attrs << "default(#{quote(col.default)})" if col.default
|
50
|
+
attrs << "not null" unless col.null
|
51
|
+
attrs << "primary key" if col.name == klass.primary_key
|
52
|
+
|
53
|
+
col_type = col.type.to_s
|
54
|
+
if col_type == "decimal"
|
55
|
+
col_type << "(#{col.precision}, #{col.scale})"
|
56
|
+
else
|
57
|
+
col_type << "(#{col.limit})" if col.limit
|
58
|
+
end
|
59
|
+
|
60
|
+
# Check out if we got a geometric column
|
61
|
+
# and print the type and SRID
|
62
|
+
if col.respond_to?(:geometry_type)
|
63
|
+
attrs << "#{col.geometry_type}, #{col.srid}"
|
64
|
+
end
|
65
|
+
|
66
|
+
cols << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
|
67
|
+
end
|
68
|
+
cols.sort! if options[:sort]
|
69
|
+
info << cols.join
|
70
|
+
|
71
|
+
if options[:show_indexes]
|
72
|
+
info << get_index_info(klass)
|
73
|
+
end
|
74
|
+
|
75
|
+
info << "#\n\n"
|
76
|
+
end
|
77
|
+
|
78
|
+
def get_index_info(klass)
|
79
|
+
index_info = "#\n# Indexes\n#\n"
|
80
|
+
|
81
|
+
indexes = klass.connection.indexes(klass.table_name)
|
82
|
+
return "" if indexes.empty?
|
83
|
+
|
84
|
+
max_size = indexes.collect{|index| index.name.size}.max + 1
|
85
|
+
indexes.each do |index|
|
86
|
+
index_info << sprintf("# %-#{max_size}.#{max_size}s %s %s", index.name, "(#{index.columns.join(",")})", index.unique ? "UNIQUE" : "").rstrip + "\n"
|
87
|
+
end
|
88
|
+
return index_info
|
89
|
+
end
|
90
|
+
|
91
|
+
# Add a schema block to a file. If the file already contains
|
92
|
+
# a schema info block (a comment starting with "== Schema Information"), check if it
|
93
|
+
# matches the block that is already there. If so, leave it be. If not, remove the old
|
94
|
+
# info block and write a new one.
|
95
|
+
# Returns true or false depending on whether the file was modified.
|
96
|
+
#
|
97
|
+
# === Options (opts)
|
98
|
+
# :position<Symbol>:: where to place the annotated section in fixture or model file,
|
99
|
+
# "before" or "after". Default is "before".
|
100
|
+
# :position_in_class<Symbol>:: where to place the annotated section in model file
|
101
|
+
# :position_in_fixture<Symbol>:: where to place the annotated section in fixture file
|
102
|
+
#
|
103
|
+
def annotate_one_file(file_name, info_block, options={})
|
104
|
+
if File.exist?(file_name)
|
105
|
+
old_content = File.read(file_name)
|
106
|
+
|
107
|
+
# Ignore the Schema version line because it changes with each migration
|
108
|
+
header = Regexp.new(/(^# Table name:.*?\n(#.*\n)*\n)/)
|
109
|
+
old_header = old_content.match(header).to_s
|
110
|
+
new_header = info_block.match(header).to_s
|
111
|
+
|
112
|
+
if old_header == new_header
|
113
|
+
false
|
114
|
+
else
|
115
|
+
# Remove old schema info
|
116
|
+
old_content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
|
117
|
+
|
118
|
+
# Write it back
|
119
|
+
new_content = ((options[:position] || :before).to_sym == :before) ? (info_block + old_content) : (old_content + "\n" + info_block)
|
120
|
+
|
121
|
+
File.open(file_name, "wb") { |f| f.puts new_content }
|
122
|
+
true
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def remove_annotation_of_file(file_name)
|
128
|
+
if File.exist?(file_name)
|
129
|
+
content = File.read(file_name)
|
130
|
+
|
131
|
+
content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
|
132
|
+
|
133
|
+
File.open(file_name, "wb") { |f| f.puts content }
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Given the name of an ActiveRecord class, create a schema
|
138
|
+
# info block (basically a comment containing information
|
139
|
+
# on the columns and their types) and put it at the front
|
140
|
+
# of the model and fixture source files.
|
141
|
+
# Returns true or false depending on whether the source
|
142
|
+
# files were modified.
|
143
|
+
|
144
|
+
def annotate(klass, file, header,options={})
|
145
|
+
info = get_schema_info(klass, header, options)
|
146
|
+
annotated = false
|
147
|
+
model_name = klass.name.underscore
|
148
|
+
model_file_name = File.join(model_dir, file)
|
149
|
+
if annotate_one_file(model_file_name, info, options.merge(
|
150
|
+
:position=>(options[:position_in_class] || options[:position])))
|
151
|
+
annotated = true
|
152
|
+
end
|
153
|
+
|
154
|
+
[
|
155
|
+
File.join(UNIT_TEST_DIR, "#{model_name}_test.rb"), # test
|
156
|
+
File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec
|
157
|
+
File.join(EXEMPLARS_DIR, "#{model_name}_exemplar.rb"), # Object Daddy
|
158
|
+
].each { |file| annotate_one_file(file, info) }
|
159
|
+
|
160
|
+
FIXTURE_DIRS.each do |dir|
|
161
|
+
fixture_file_name = File.join(dir,klass.table_name + ".yml")
|
162
|
+
annotate_one_file(fixture_file_name, info, options.merge(:position=>(options[:position_in_fixture] || options[:position]))) if File.exist?(fixture_file_name)
|
163
|
+
end
|
164
|
+
annotated
|
165
|
+
end
|
166
|
+
|
167
|
+
# Return a list of the model files to annotate. If we have
|
168
|
+
# command line arguments, they're assumed to be either
|
169
|
+
# the underscore or CamelCase versions of model names.
|
170
|
+
# Otherwise we take all the model files in the
|
171
|
+
# model_dir directory.
|
172
|
+
def get_model_files
|
173
|
+
models = ARGV.dup
|
174
|
+
models.shift
|
175
|
+
models.reject!{|m| m.starts_with?("position=")}
|
176
|
+
if models.empty?
|
177
|
+
Dir.chdir(model_dir) do
|
178
|
+
models = Dir["**/*.rb"]
|
179
|
+
end
|
180
|
+
end
|
181
|
+
models
|
182
|
+
end
|
183
|
+
|
184
|
+
# Retrieve the classes belonging to the model names we're asked to process
|
185
|
+
# Check for namespaced models in subdirectories as well as models
|
186
|
+
# in subdirectories without namespacing.
|
187
|
+
def get_model_class(file)
|
188
|
+
require "#{model_dir}/#{file}" # this is for non-rails projects, which don't get Rails auto-require magic
|
189
|
+
model = file.gsub(/\.rb$/, '').camelize
|
190
|
+
parts = model.split('::')
|
191
|
+
begin
|
192
|
+
parts.inject(Object) {|klass, part| klass.const_get(part) }
|
193
|
+
rescue LoadError
|
194
|
+
Object.const_get(parts.last)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# We're passed a name of things that might be
|
199
|
+
# ActiveRecord models. If we can find the class, and
|
200
|
+
# if its a subclass of ActiveRecord::Base,
|
201
|
+
# then pas it to the associated block
|
202
|
+
def do_annotations(options={})
|
203
|
+
header = PREFIX.dup
|
204
|
+
|
205
|
+
if options[:include_version]
|
206
|
+
version = ActiveRecord::Migrator.current_version rescue 0
|
207
|
+
if version > 0
|
208
|
+
header << "\n# Schema version: #{version}"
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
if options[:model_dir]
|
213
|
+
self.model_dir = options[:model_dir]
|
214
|
+
end
|
215
|
+
|
216
|
+
annotated = []
|
217
|
+
get_model_files.each do |file|
|
218
|
+
begin
|
219
|
+
klass = get_model_class(file)
|
220
|
+
if klass < ActiveRecord::Base && !klass.abstract_class?
|
221
|
+
if annotate(klass, file, header,options)
|
222
|
+
annotated << klass
|
223
|
+
end
|
224
|
+
end
|
225
|
+
rescue Exception => e
|
226
|
+
puts "Unable to annotate #{file}: #{e.message} (#{e.backtrace.first})"
|
227
|
+
end
|
228
|
+
end
|
229
|
+
if annotated.empty?
|
230
|
+
puts "Nothing annotated!"
|
231
|
+
else
|
232
|
+
puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
def remove_annotations(options={})
|
237
|
+
p options
|
238
|
+
if options[:model_dir]
|
239
|
+
puts "removing"
|
240
|
+
self.model_dir = options[:model_dir]
|
241
|
+
end
|
242
|
+
deannotated = []
|
243
|
+
get_model_files.each do |file|
|
244
|
+
begin
|
245
|
+
klass = get_model_class(file)
|
246
|
+
if klass < ActiveRecord::Base && !klass.abstract_class?
|
247
|
+
deannotated << klass
|
248
|
+
|
249
|
+
model_file_name = File.join(model_dir, file)
|
250
|
+
remove_annotation_of_file(model_file_name)
|
251
|
+
|
252
|
+
FIXTURE_DIRS.each do |dir|
|
253
|
+
fixture_file_name = File.join(dir,klass.table_name + ".yml")
|
254
|
+
remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name)
|
255
|
+
end
|
256
|
+
end
|
257
|
+
rescue Exception => e
|
258
|
+
puts "Unable to annotate #{file}: #{e.message}"
|
259
|
+
end
|
260
|
+
end
|
261
|
+
puts "Removed annotation from: #{deannotated.join(', ')}"
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# == Annotate Routes
|
2
|
+
#
|
3
|
+
# Based on:
|
4
|
+
#
|
5
|
+
#
|
6
|
+
#
|
7
|
+
# Prepends the output of "rake routes" to the top of your routes.rb file.
|
8
|
+
# Yes, it's simple but I'm thick and often need a reminder of what my routes mean.
|
9
|
+
#
|
10
|
+
# Running this task will replace any exising route comment generated by the task.
|
11
|
+
# Best to back up your routes file before running:
|
12
|
+
#
|
13
|
+
# Author:
|
14
|
+
# Gavin Montague
|
15
|
+
# gavin@leftbrained.co.uk
|
16
|
+
#
|
17
|
+
# Released under the same license as Ruby. No Support. No Warranty.module AnnotateRoutes
|
18
|
+
#
|
19
|
+
module AnnotateRoutes
|
20
|
+
PREFIX = "#== Route Map"
|
21
|
+
|
22
|
+
def self.do_annotate
|
23
|
+
routes_rb = File.join("config", "routes.rb")
|
24
|
+
header = PREFIX + "\n# Generated on #{Time.now.strftime("%d %b %Y %H:%M")}\n#"
|
25
|
+
if File.exists? routes_rb
|
26
|
+
routes_map = `rake routes`
|
27
|
+
routes_map = routes_map.split("\n")
|
28
|
+
routes_map.shift # remove the first line of rake routes which is just a file path
|
29
|
+
routes_map = routes_map.inject(header){|sum, line| sum<<"\n# "<<line}
|
30
|
+
content = File.read(routes_rb)
|
31
|
+
content, old = content.split(/^#== Route .*?\n/)
|
32
|
+
File.open(routes_rb, "wb") do |f|
|
33
|
+
f.puts content.sub!(/\n?\z/, "\n") + routes_map
|
34
|
+
end
|
35
|
+
puts "Route file annotated."
|
36
|
+
else
|
37
|
+
puts "Can`t find routes.rb"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
desc "Add schema information (as comments) to model and fixture files"
|
2
|
+
task :annotate_models => :environment do
|
3
|
+
require 'annotate/annotate_models'
|
4
|
+
options={}
|
5
|
+
options[:position_in_class] = ENV['position_in_class'] || ENV['position'] || :before
|
6
|
+
options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position'] || :before
|
7
|
+
options[:show_indexes] = ENV['show_indexes']
|
8
|
+
options[:model_dir] = ENV['model_dir']
|
9
|
+
options[:include_version] = ENV['include_version']
|
10
|
+
options[:sort] = ENV['sort']
|
11
|
+
AnnotateModels.do_annotations(options)
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Remove schema information from model and fixture files"
|
15
|
+
task :remove_annotation => :environment do
|
16
|
+
require 'annotate/annotate_models'
|
17
|
+
options={}
|
18
|
+
options[:model_dir] = ENV['model_dir']
|
19
|
+
AnnotateModels.remove_annotations(options)
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tonyday-annotate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tony Day
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-23 00:00:00 -07:00
|
13
|
+
default_executable: annotate
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: newgem
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.4.1
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: hoe
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.8.0
|
34
|
+
version:
|
35
|
+
description: Annotates Rails Models, routes, and others
|
36
|
+
email:
|
37
|
+
- tonyd@panztell.com
|
38
|
+
executables:
|
39
|
+
- annotate
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- History.txt
|
44
|
+
- README.rdoc
|
45
|
+
files:
|
46
|
+
- History.txt
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- annotate_models.gemspec
|
50
|
+
- bin/annotate
|
51
|
+
- lib/annotate.rb
|
52
|
+
- lib/annotate/annotate_models.rb
|
53
|
+
- lib/annotate/annotate_routes.rb
|
54
|
+
- lib/tasks/annotate_models.rake
|
55
|
+
- lib/tasks/annotate_routes.rake
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/tonyday/annotate_models
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --main
|
61
|
+
- README.rdoc
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project: annotate-models
|
79
|
+
rubygems_version: 1.2.0
|
80
|
+
signing_key:
|
81
|
+
specification_version: 2
|
82
|
+
summary: Annotates Rails Models, routes, and others
|
83
|
+
test_files: []
|
84
|
+
|