forked from ctran/annotate_models
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first import from http://repo.pragprog.com/svn/Public/plugins/annotat…
- Loading branch information
Cuong Tran
committed
Feb 27, 2008
0 parents
commit 1da0386
Showing
50 changed files
with
5,334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
== 0.0.1 2008-02-27 | ||
|
||
* 1 major enhancement: | ||
* Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2008 FIXME full name | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
History.txt | ||
License.txt | ||
Manifest.txt | ||
README.txt | ||
Rakefile | ||
config/hoe.rb | ||
config/requirements.rb | ||
lib/annotate_models.rb | ||
lib/annotate_models/version.rb | ||
log/debug.log | ||
script/destroy | ||
script/generate | ||
script/txt2html | ||
setup.rb | ||
tasks/deployment.rake | ||
tasks/environment.rake | ||
tasks/website.rake | ||
test/test_annotate_models.rb | ||
test/test_helper.rb | ||
website/index.html | ||
website/index.txt | ||
website/javascripts/rounded_corners_lite.inc.js | ||
website/stylesheets/screen.css | ||
website/template.rhtml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
AnnotateModels | ||
============== | ||
|
||
Add a comment summarizing the current schema to the top of each ActiveRecord model source file. | ||
|
||
# Schema as of Sun Feb 26 21:58:32 CST 2006 (schema version 7) | ||
# | ||
# id :integer(11) not null | ||
# quantity :integer(11) | ||
# product_id :integer(11) | ||
# unit_price :float | ||
# order_id :integer(11) | ||
# | ||
|
||
class LineItem < ActiveRecord::Base | ||
belongs_to :product | ||
|
||
end | ||
|
||
Note that this code will blow away the initial comment block in your models if it looks like it was | ||
previously added by annotate models, so you don't want to add additional text to an automatically | ||
created comment block. | ||
|
||
Author: | ||
Dave Thomas | ||
Pragmatic Programmers, LLC | ||
|
||
Released under the same license as Ruby. No Support. No Warranty. | ||
|
||
Modifications by: | ||
- [email protected] | ||
- [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'config/requirements' | ||
require 'config/hoe' # setup Hoe + all gem configuration | ||
|
||
Dir['tasks/**/*.rake'].each { |rake| load rake } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'annotate_models/version' | ||
|
||
AUTHOR = 'Dave Thomas' # can also be an array of Authors | ||
EMAIL = "[email protected]" | ||
DESCRIPTION = "Add a comment summarizing the current schema to the top of each ActiveRecord model source file" | ||
GEM_NAME = 'annotate_models' # what ppl will type to install your gem | ||
RUBYFORGE_PROJECT = 'annotate-models' # The unix name for your project | ||
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org" | ||
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}" | ||
|
||
@config_file = "~/.rubyforge/user-config.yml" | ||
@config = nil | ||
RUBYFORGE_USERNAME = "unknown" | ||
def rubyforge_username | ||
unless @config | ||
begin | ||
@config = YAML.load(File.read(File.expand_path(@config_file))) | ||
rescue | ||
puts <<-EOS | ||
ERROR: No rubyforge config file found: #{@config_file} | ||
Run 'rubyforge setup' to prepare your env for access to Rubyforge | ||
- See http://newgem.rubyforge.org/rubyforge.html for more details | ||
EOS | ||
exit | ||
end | ||
end | ||
RUBYFORGE_USERNAME.replace @config["username"] | ||
end | ||
|
||
|
||
REV = nil | ||
# UNCOMMENT IF REQUIRED: | ||
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil | ||
VERS = AnnotateModels::VERSION::STRING + (REV ? ".#{REV}" : "") | ||
RDOC_OPTS = ['--quiet', '--title', 'annotate_models documentation', | ||
"--opname", "index.html", | ||
"--line-numbers", | ||
"--main", "README", | ||
"--inline-source"] | ||
|
||
class Hoe | ||
def extra_deps | ||
@extra_deps.reject! { |x| Array(x).first == 'hoe' } | ||
@extra_deps | ||
end | ||
end | ||
|
||
# Generate all the Rake tasks | ||
# Run 'rake -T' to see list of generated tasks (from gem root directory) | ||
hoe = Hoe.new(GEM_NAME, VERS) do |p| | ||
p.developer(AUTHOR, EMAIL) | ||
p.description = DESCRIPTION | ||
p.summary = DESCRIPTION | ||
p.url = HOMEPATH | ||
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT | ||
p.test_globs = ["test/**/test_*.rb"] | ||
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean. | ||
|
||
# == Optional | ||
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n") | ||
#p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ] | ||
|
||
#p.spec_extras = {} # A hash of extra values to set in the gemspec. | ||
|
||
end | ||
|
||
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n") | ||
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}" | ||
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc') | ||
hoe.rsync_args = '-av --delete --ignore-errors' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'fileutils' | ||
include FileUtils | ||
|
||
require 'rubygems' | ||
%w[rake hoe newgem rubigen].each do |req_gem| | ||
begin | ||
require req_gem | ||
rescue LoadError | ||
puts "This Rakefile requires the '#{req_gem}' RubyGem." | ||
puts "Installation: gem install #{req_gem} -y" | ||
exit | ||
end | ||
end | ||
|
||
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib])) | ||
|
||
require 'annotate_models' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
$:.unshift File.dirname(__FILE__) | ||
|
||
module AnnotateModels | ||
RAILS_ROOT = '.' | ||
MODEL_DIR = File.join(RAILS_ROOT, "app/models") | ||
FIXTURE_DIR = File.join(RAILS_ROOT, "test/fixtures") | ||
PREFIX = "== Schema Information" | ||
|
||
# Simple quoting for the default column value | ||
def self.quote(value) | ||
case value | ||
when NilClass then "NULL" | ||
when TrueClass then "TRUE" | ||
when FalseClass then "FALSE" | ||
when Float, Fixnum, Bignum then value.to_s | ||
# BigDecimals need to be output in a non-normalized form and quoted. | ||
when BigDecimal then value.to_s('F') | ||
else | ||
value.inspect | ||
end | ||
end | ||
|
||
# Use the column information in an ActiveRecord class | ||
# to create a comment block containing a line for | ||
# each column. The line contains the column name, | ||
# the type (and length), and any optional attributes | ||
def self.get_schema_info(klass, header) | ||
info = "# #{header}\n#\n" | ||
info << "# Table name: #{klass.table_name}\n#\n" | ||
|
||
max_size = klass.column_names.collect{|name| name.size}.max + 1 | ||
klass.columns.each do |col| | ||
attrs = [] | ||
attrs << "default(#{quote(col.default)})" if col.default | ||
attrs << "not null" unless col.null | ||
attrs << "primary key" if col.name == klass.primary_key | ||
|
||
col_type = col.type.to_s | ||
if col_type == "decimal" | ||
col_type << "(#{col.precision}, #{col.scale})" | ||
else | ||
col_type << "(#{col.limit})" if col.limit | ||
end | ||
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s\n", col.name, col_type, attrs.join(", ")) | ||
end | ||
|
||
info << "#\n\n" | ||
end | ||
|
||
# Add a schema block to a file. If the file already contains | ||
# a schema info block (a comment starting | ||
# with "Schema as of ..."), remove it first. | ||
|
||
def self.annotate_one_file(file_name, info_block) | ||
if File.exist?(file_name) | ||
content = File.read(file_name) | ||
|
||
# Remove old schema info | ||
content.sub!(/^# #{PREFIX}.*?\n(#.*\n)*\n/, '') | ||
|
||
# Write it back | ||
File.open(file_name, "w") { |f| f.puts info_block + content } | ||
end | ||
end | ||
|
||
# Given the name of an ActiveRecord class, create a schema | ||
# info block (basically a comment containing information | ||
# on the columns and their types) and put it at the front | ||
# of the model and fixture source files. | ||
|
||
def self.annotate(klass, header) | ||
info = get_schema_info(klass, header) | ||
|
||
model_file_name = File.join(MODEL_DIR, klass.name.underscore + ".rb") | ||
annotate_one_file(model_file_name, info) | ||
|
||
fixture_file_name = File.join(FIXTURE_DIR, klass.table_name + ".yml") | ||
annotate_one_file(fixture_file_name, info) | ||
end | ||
|
||
# Return a list of the model files to annotate. If we have | ||
# command line arguments, they're assumed to be either | ||
# the underscore or CamelCase versions of model names. | ||
# Otherwise we take all the model files in the | ||
# app/models directory. | ||
def self.get_model_names | ||
models = ARGV.dup | ||
models.shift | ||
|
||
if models.empty? | ||
Dir.chdir(MODEL_DIR) do | ||
models = Dir["**/*.rb"] | ||
end | ||
end | ||
models | ||
end | ||
|
||
# We're passed a name of things that might be | ||
# ActiveRecord models. If we can find the class, and | ||
# if its a subclass of ActiveRecord::Base, | ||
# then pas it to the associated block | ||
|
||
def self.do_annotations | ||
header = PREFIX.dup | ||
version = ActiveRecord::Migrator.current_version rescue 0 | ||
if version > 0 | ||
header << "\n# Schema version: #{version}" | ||
end | ||
|
||
annotated = [] | ||
self.get_model_names.each do |m| | ||
class_name = m.sub(/\.rb$/,'').camelize | ||
begin | ||
klass = class_name.split('::').inject(Object){ |klass,part| klass.const_get(part) } | ||
if klass < ActiveRecord::Base && !klass.abstract_class? | ||
annotated << class_name | ||
self.annotate(klass, header) | ||
end | ||
rescue Exception => e | ||
puts "Unable to annotate #{class_name}: #{e.message}" | ||
end | ||
|
||
end | ||
puts "Annotated #{annotated.join(', ')}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module AnnotateModels #:nodoc: | ||
module VERSION #:nodoc: | ||
MAJOR = 0 | ||
MINOR = 0 | ||
TINY = 1 | ||
|
||
STRING = [MAJOR, MINOR, TINY].join('.') | ||
end | ||
end |
Empty file.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
== 0.0.1 2008-02-27 | ||
|
||
* 1 major enhancement: | ||
* Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2008 FIXME full name | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
History.txt | ||
License.txt | ||
Manifest.txt | ||
README.txt | ||
Rakefile | ||
config/hoe.rb | ||
config/requirements.rb | ||
lib/annotate_models.rb | ||
lib/annotate_models/version.rb | ||
log/debug.log | ||
script/destroy | ||
script/generate | ||
script/txt2html | ||
setup.rb | ||
tasks/deployment.rake | ||
tasks/environment.rake | ||
tasks/website.rake | ||
test/test_annotate_models.rb | ||
test/test_helper.rb | ||
website/index.html | ||
website/index.txt | ||
website/javascripts/rounded_corners_lite.inc.js | ||
website/stylesheets/screen.css | ||
website/template.rhtml |
Oops, something went wrong.