Skip to content

Commit 42b2d4d

Browse files
committed
rotuka nice code and a readme update
1 parent ee098af commit 42b2d4d

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

README

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
AnnotateSchema
22
==============
33

4-
Add a comment summarizing the current schema to the top
4+
Add a comment summarizing the current schema to the bottom
55
of each ActiveRecord model source file:
66

77
# Schema as of Sun Feb 26 21:58:32 CST 2006 (schema version 7)
@@ -17,15 +17,29 @@ of each ActiveRecord model source file:
1717

1818
. . .
1919

20-
Note that this code will blow away the initial comment block in your models if it looks ike it was
20+
Note that this code will blow away the initial/final comment block in your models if it looks ike it was
2121
previously added by annotate models, so you don't want to add additional text to an automatically
2222
created comment block.
2323

24+
25+
== How to use:
26+
27+
Will annotate all your models:
28+
29+
rake db:annotate
30+
31+
Options:
32+
Annotate on the head of the file:
33+
34+
rake db:annotate POSITION='top'
35+
36+
37+
== License:
38+
2439
Author:
2540
Dave Thomas
2641
Pragmatic Programmers, LLC
2742

2843
Released under the same license as Ruby. No Support. No Warranty.
2944

30-
Back up your model files before using...
31-
45+
Back up your model files before using...

lib/annotate_models.rb

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
require "config/environment"
22

3-
MODEL_DIR = File.join(RAILS_ROOT, "app/models")
3+
MODEL_DIR = File.join(RAILS_ROOT, "app/models" )
44
SPEC_MODEL_DIR = File.join(RAILS_ROOT, "spec/models")
5-
FIXTURE_DIR = File.join(RAILS_ROOT, "#{ENV['FIXTURES'] ? ENV['FIXTURES'] : "test"}/fixtures")
6-
SORT_COLUMNS = ENV['SORT'] ? ENV['SORT'] != 'no' : true
5+
UNIT_TEST_DIR = File.join(RAILS_ROOT, "test/unit" )
6+
FIXTURE_DIR = ENV['FIXTURES'] ? "#{ENV['FIXTURES']}/fixtures" : [File.join(RAILS_ROOT, "spec/fixtures"), File.join(RAILS_ROOT, "test/fixtures")].detect { |dir| File.exists?(dir) }
7+
SORT_COLUMNS = ENV['SORT'] ? ENV['SORT'] != 'no' : true
78

89
module AnnotateModels
910

@@ -43,22 +44,22 @@ def self.get_schema_info(klass, header)
4344
klass.columns
4445
end.each { |col| info << annotate_column(col, klass, max_size) }
4546

46-
info << "#\n\n"
47+
info << "\n"
4748
end
4849

4950
def self.annotate_column(col, klass, max_size)
5051
attrs = []
51-
attrs << "default(#{quote(col.default)})" if col.default
52-
attrs << "not null" unless col.null
52+
attrs << "not null" unless col.null
53+
attrs << "default(#{quote(col.default)})" if col.default
5354
attrs << "primary key" if col.name == klass.primary_key
54-
55+
5556
col_type = col.type.to_s
5657
if col_type == "decimal"
5758
col_type << "(#{col.precision}, #{col.scale})"
5859
else
5960
col_type << "(#{col.limit})" if col.limit
6061
end
61-
sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s\n", col.name, col_type, attrs.join(", "))
62+
sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip << "\n"
6263
end
6364

6465
# Add a schema block to a file. If the file already contains
@@ -75,7 +76,7 @@ def self.annotate_one_file(file_name, info_block)
7576

7677
# Write it back
7778
File.open(file_name, "w") do |f|
78-
f.puts ENV['POSITION'] == 'top' ? info_block + content : content + "\n" + info_block
79+
f.puts ENV['POSITION'] == 'top' ? info_block + content : content + info_block
7980
end
8081
end
8182
end
@@ -88,14 +89,12 @@ def self.annotate_one_file(file_name, info_block)
8889
def self.annotate(klass, header)
8990
info = get_schema_info(klass, header)
9091

91-
model_file_name = File.join(MODEL_DIR, klass.name.underscore + ".rb")
92-
annotate_one_file(model_file_name, info)
93-
94-
rspec_model_file_name = File.join(SPEC_MODEL_DIR, klass.name.underscore + "_spec.rb")
95-
annotate_one_file(rspec_model_file_name, info)
96-
97-
fixture_file_name = File.join(FIXTURE_DIR, klass.table_name + ".yml")
98-
annotate_one_file(fixture_file_name, info)
92+
[
93+
File.join(MODEL_DIR, klass.name.underscore + ".rb"), # model
94+
File.join(SPEC_MODEL_DIR, klass.name.underscore + "_spec.rb"), # spec
95+
File.join(UNIT_TEST_DIR, klass.name.underscore + "_test.rb"), # test
96+
File.join(FIXTURE_DIR, klass.table_name + ".yml"), # fixture
97+
].each { |file| annotate_one_file(file, info) }
9998
end
10099

101100
# Return a list of the model files to annotate. If we have

0 commit comments

Comments
 (0)