Skip to content

Commit 1da0386

Browse files
author
Cuong Tran
committed
0 parents  commit 1da0386

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+5334
-0
lines changed

History.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
== 0.0.1 2008-02-27
2+
3+
* 1 major enhancement:
4+
* Initial release

License.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2008 FIXME full name
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Manifest.txt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
History.txt
2+
License.txt
3+
Manifest.txt
4+
README.txt
5+
Rakefile
6+
config/hoe.rb
7+
config/requirements.rb
8+
lib/annotate_models.rb
9+
lib/annotate_models/version.rb
10+
log/debug.log
11+
script/destroy
12+
script/generate
13+
script/txt2html
14+
setup.rb
15+
tasks/deployment.rake
16+
tasks/environment.rake
17+
tasks/website.rake
18+
test/test_annotate_models.rb
19+
test/test_helper.rb
20+
website/index.html
21+
website/index.txt
22+
website/javascripts/rounded_corners_lite.inc.js
23+
website/stylesheets/screen.css
24+
website/template.rhtml

README.txt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
AnnotateModels
2+
==============
3+
4+
Add a comment summarizing the current schema to the top of each ActiveRecord model source file.
5+
6+
# Schema as of Sun Feb 26 21:58:32 CST 2006 (schema version 7)
7+
#
8+
# id :integer(11) not null
9+
# quantity :integer(11)
10+
# product_id :integer(11)
11+
# unit_price :float
12+
# order_id :integer(11)
13+
#
14+
15+
class LineItem < ActiveRecord::Base
16+
belongs_to :product
17+
18+
end
19+
20+
Note that this code will blow away the initial comment block in your models if it looks like it was
21+
previously added by annotate models, so you don't want to add additional text to an automatically
22+
created comment block.
23+
24+
Author:
25+
Dave Thomas
26+
Pragmatic Programmers, LLC
27+
28+
Released under the same license as Ruby. No Support. No Warranty.
29+
30+
Modifications by:
31+
32+

Rakefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'config/requirements'
2+
require 'config/hoe' # setup Hoe + all gem configuration
3+
4+
Dir['tasks/**/*.rake'].each { |rake| load rake }

config/hoe.rb

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
require 'annotate_models/version'
2+
3+
AUTHOR = 'Dave Thomas' # can also be an array of Authors
4+
5+
DESCRIPTION = "Add a comment summarizing the current schema to the top of each ActiveRecord model source file"
6+
GEM_NAME = 'annotate_models' # what ppl will type to install your gem
7+
RUBYFORGE_PROJECT = 'annotate-models' # The unix name for your project
8+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10+
11+
@config_file = "~/.rubyforge/user-config.yml"
12+
@config = nil
13+
RUBYFORGE_USERNAME = "unknown"
14+
def rubyforge_username
15+
unless @config
16+
begin
17+
@config = YAML.load(File.read(File.expand_path(@config_file)))
18+
rescue
19+
puts <<-EOS
20+
ERROR: No rubyforge config file found: #{@config_file}
21+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
22+
- See http://newgem.rubyforge.org/rubyforge.html for more details
23+
EOS
24+
exit
25+
end
26+
end
27+
RUBYFORGE_USERNAME.replace @config["username"]
28+
end
29+
30+
31+
REV = nil
32+
# UNCOMMENT IF REQUIRED:
33+
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34+
VERS = AnnotateModels::VERSION::STRING + (REV ? ".#{REV}" : "")
35+
RDOC_OPTS = ['--quiet', '--title', 'annotate_models documentation',
36+
"--opname", "index.html",
37+
"--line-numbers",
38+
"--main", "README",
39+
"--inline-source"]
40+
41+
class Hoe
42+
def extra_deps
43+
@extra_deps.reject! { |x| Array(x).first == 'hoe' }
44+
@extra_deps
45+
end
46+
end
47+
48+
# Generate all the Rake tasks
49+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
50+
hoe = Hoe.new(GEM_NAME, VERS) do |p|
51+
p.developer(AUTHOR, EMAIL)
52+
p.description = DESCRIPTION
53+
p.summary = DESCRIPTION
54+
p.url = HOMEPATH
55+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
56+
p.test_globs = ["test/**/test_*.rb"]
57+
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
58+
59+
# == Optional
60+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
61+
#p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
62+
63+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
64+
65+
end
66+
67+
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
68+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
69+
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
70+
hoe.rsync_args = '-av --delete --ignore-errors'

config/requirements.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'fileutils'
2+
include FileUtils
3+
4+
require 'rubygems'
5+
%w[rake hoe newgem rubigen].each do |req_gem|
6+
begin
7+
require req_gem
8+
rescue LoadError
9+
puts "This Rakefile requires the '#{req_gem}' RubyGem."
10+
puts "Installation: gem install #{req_gem} -y"
11+
exit
12+
end
13+
end
14+
15+
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
16+
17+
require 'annotate_models'

lib/annotate_models.rb

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
$:.unshift File.dirname(__FILE__)
2+
3+
module AnnotateModels
4+
RAILS_ROOT = '.'
5+
MODEL_DIR = File.join(RAILS_ROOT, "app/models")
6+
FIXTURE_DIR = File.join(RAILS_ROOT, "test/fixtures")
7+
PREFIX = "== Schema Information"
8+
9+
# Simple quoting for the default column value
10+
def self.quote(value)
11+
case value
12+
when NilClass then "NULL"
13+
when TrueClass then "TRUE"
14+
when FalseClass then "FALSE"
15+
when Float, Fixnum, Bignum then value.to_s
16+
# BigDecimals need to be output in a non-normalized form and quoted.
17+
when BigDecimal then value.to_s('F')
18+
else
19+
value.inspect
20+
end
21+
end
22+
23+
# Use the column information in an ActiveRecord class
24+
# to create a comment block containing a line for
25+
# each column. The line contains the column name,
26+
# the type (and length), and any optional attributes
27+
def self.get_schema_info(klass, header)
28+
info = "# #{header}\n#\n"
29+
info << "# Table name: #{klass.table_name}\n#\n"
30+
31+
max_size = klass.column_names.collect{|name| name.size}.max + 1
32+
klass.columns.each do |col|
33+
attrs = []
34+
attrs << "default(#{quote(col.default)})" if col.default
35+
attrs << "not null" unless col.null
36+
attrs << "primary key" if col.name == klass.primary_key
37+
38+
col_type = col.type.to_s
39+
if col_type == "decimal"
40+
col_type << "(#{col.precision}, #{col.scale})"
41+
else
42+
col_type << "(#{col.limit})" if col.limit
43+
end
44+
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s\n", col.name, col_type, attrs.join(", "))
45+
end
46+
47+
info << "#\n\n"
48+
end
49+
50+
# Add a schema block to a file. If the file already contains
51+
# a schema info block (a comment starting
52+
# with "Schema as of ..."), remove it first.
53+
54+
def self.annotate_one_file(file_name, info_block)
55+
if File.exist?(file_name)
56+
content = File.read(file_name)
57+
58+
# Remove old schema info
59+
content.sub!(/^# #{PREFIX}.*?\n(#.*\n)*\n/, '')
60+
61+
# Write it back
62+
File.open(file_name, "w") { |f| f.puts info_block + content }
63+
end
64+
end
65+
66+
# Given the name of an ActiveRecord class, create a schema
67+
# info block (basically a comment containing information
68+
# on the columns and their types) and put it at the front
69+
# of the model and fixture source files.
70+
71+
def self.annotate(klass, header)
72+
info = get_schema_info(klass, header)
73+
74+
model_file_name = File.join(MODEL_DIR, klass.name.underscore + ".rb")
75+
annotate_one_file(model_file_name, info)
76+
77+
fixture_file_name = File.join(FIXTURE_DIR, klass.table_name + ".yml")
78+
annotate_one_file(fixture_file_name, info)
79+
end
80+
81+
# Return a list of the model files to annotate. If we have
82+
# command line arguments, they're assumed to be either
83+
# the underscore or CamelCase versions of model names.
84+
# Otherwise we take all the model files in the
85+
# app/models directory.
86+
def self.get_model_names
87+
models = ARGV.dup
88+
models.shift
89+
90+
if models.empty?
91+
Dir.chdir(MODEL_DIR) do
92+
models = Dir["**/*.rb"]
93+
end
94+
end
95+
models
96+
end
97+
98+
# We're passed a name of things that might be
99+
# ActiveRecord models. If we can find the class, and
100+
# if its a subclass of ActiveRecord::Base,
101+
# then pas it to the associated block
102+
103+
def self.do_annotations
104+
header = PREFIX.dup
105+
version = ActiveRecord::Migrator.current_version rescue 0
106+
if version > 0
107+
header << "\n# Schema version: #{version}"
108+
end
109+
110+
annotated = []
111+
self.get_model_names.each do |m|
112+
class_name = m.sub(/\.rb$/,'').camelize
113+
begin
114+
klass = class_name.split('::').inject(Object){ |klass,part| klass.const_get(part) }
115+
if klass < ActiveRecord::Base && !klass.abstract_class?
116+
annotated << class_name
117+
self.annotate(klass, header)
118+
end
119+
rescue Exception => e
120+
puts "Unable to annotate #{class_name}: #{e.message}"
121+
end
122+
123+
end
124+
puts "Annotated #{annotated.join(', ')}"
125+
end
126+
end

lib/annotate_models/version.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module AnnotateModels #:nodoc:
2+
module VERSION #:nodoc:
3+
MAJOR = 0
4+
MINOR = 0
5+
TINY = 1
6+
7+
STRING = [MAJOR, MINOR, TINY].join('.')
8+
end
9+
end

log/debug.log

Whitespace-only changes.

pkg/annotate_models-0.0.1.gem

27 KB
Binary file not shown.

pkg/annotate_models-0.0.1.tgz

24.5 KB
Binary file not shown.

pkg/annotate_models-0.0.1/History.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
== 0.0.1 2008-02-27
2+
3+
* 1 major enhancement:
4+
* Initial release

pkg/annotate_models-0.0.1/License.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2008 FIXME full name
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
History.txt
2+
License.txt
3+
Manifest.txt
4+
README.txt
5+
Rakefile
6+
config/hoe.rb
7+
config/requirements.rb
8+
lib/annotate_models.rb
9+
lib/annotate_models/version.rb
10+
log/debug.log
11+
script/destroy
12+
script/generate
13+
script/txt2html
14+
setup.rb
15+
tasks/deployment.rake
16+
tasks/environment.rake
17+
tasks/website.rake
18+
test/test_annotate_models.rb
19+
test/test_helper.rb
20+
website/index.html
21+
website/index.txt
22+
website/javascripts/rounded_corners_lite.inc.js
23+
website/stylesheets/screen.css
24+
website/template.rhtml

0 commit comments

Comments
 (0)