public
Description: Annotate ActiveRecord models as a gem
Homepage: http://agilewebdevelopment.com/plugins/annotate_models
Clone URL: git://github.com/ctran/annotate_models.git
added an option postion to choose to put the annotation, spec/fixtures now 
also get annotated, added a task to remove the annotations
Michael Bumann (author)
Fri Mar 21 18:43:58 -0700 2008
commit  7b5d2ed74fa2f4161dbd15ea838c6886a3839650
tree    2517fe262d7552fa3ccdafadec1dcc359c2d4f01
parent  12b7893be9eabfb6b8bdce87265dbcd781f7c60d
...
1
2
3
4
 
5
6
7
...
49
50
51
52
 
53
54
55
...
57
58
59
60
 
 
 
 
 
 
 
 
 
 
 
 
61
62
63
...
66
67
68
69
 
70
71
72
73
 
74
75
76
 
 
 
 
77
78
79
...
84
85
86
87
 
88
89
90
...
110
111
112
113
 
114
115
116
...
123
124
125
126
 
127
128
129
...
131
132
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
135
136
...
1
2
3
 
4
5
6
7
...
49
50
51
 
52
53
54
55
...
57
58
59
 
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
...
77
78
79
 
80
81
82
83
 
84
85
 
 
86
87
88
89
90
91
92
...
97
98
99
 
100
101
102
103
...
123
124
125
 
126
127
128
129
...
136
137
138
 
139
140
141
142
...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
0
@@ -1,7 +1,7 @@
0
 module AnnotateModels
0
   class << self
0
     MODEL_DIR = "app/models"
0
- FIXTURE_DIR = "test/fixtures"
0
+ FIXTURE_DIRS = ["test/fixtures","spec/fixtures"]
0
     PREFIX = "== Schema Information"
0
 
0
     # Simple quoting for the default column value
0
@@ -49,7 +49,7 @@ module AnnotateModels
0
     # a schema info block (a comment starting
0
     # with "Schema as of ..."), remove it first.
0
 
0
- def annotate_one_file(file_name, info_block)
0
+ def annotate_one_file(file_name, info_block, options={})
0
       if File.exist?(file_name)
0
         content = File.read(file_name)
0
 
0
@@ -57,7 +57,18 @@ module AnnotateModels
0
         content.sub!(/^# #{PREFIX}.*?\n(#.*\n)*\n/, '')
0
 
0
         # Write it back
0
- File.open(file_name, "w") { |f| f.puts info_block + content }
0
+ new_content = options[:position] == "before" ? (info_block + content) : (content + info_block)
0
+ File.open(file_name, "w") { |f| f.puts new_content }
0
+ end
0
+ end
0
+
0
+ def remove_annotation_of_file(file_name)
0
+ if File.exist?(file_name)
0
+ content = File.read(file_name)
0
+
0
+ content.sub!(/^# #{PREFIX}.*?\n(#.*\n)*\n/, '')
0
+
0
+ File.open(file_name, "w") { |f| f.puts content }
0
       end
0
     end
0
 
0
@@ -66,14 +77,16 @@ module AnnotateModels
0
     # on the columns and their types) and put it at the front
0
     # of the model and fixture source files.
0
 
0
- def annotate(klass, file, header)
0
+ def annotate(klass, file, header,options={})
0
       info = get_schema_info(klass, header)
0
 
0
       model_file_name = File.join(MODEL_DIR, file)
0
- annotate_one_file(model_file_name, info)
0
+ annotate_one_file(model_file_name, info, options.merge(:position=>(options[:position_in_class] || options[:position])))
0
 
0
- fixture_file_name = File.join(FIXTURE_DIR, klass.table_name + ".yml")
0
- annotate_one_file(fixture_file_name, info)
0
+ FIXTURE_DIRS.each do |dir|
0
+ fixture_file_name = File.join(dir,klass.table_name + ".yml")
0
+ annotate_one_file(fixture_file_name, info, options.merge(:position=>(options[:position_in_fixture] || options[:position]))) if File.exist?(fixture_file_name)
0
+ end
0
     end
0
 
0
     # Return a list of the model files to annotate. If we have
0
@@ -84,7 +97,7 @@ module AnnotateModels
0
     def get_model_files
0
       models = ARGV.dup
0
       models.shift
0
-
0
+ models.reject!{|m| m.starts_with?("position=")}
0
       if models.empty?
0
         Dir.chdir(MODEL_DIR) do
0
           models = Dir["**/*.rb"]
0
@@ -110,7 +123,7 @@ module AnnotateModels
0
     # ActiveRecord models. If we can find the class, and
0
     # if its a subclass of ActiveRecord::Base,
0
     # then pas it to the associated block
0
- def do_annotations
0
+ def do_annotations(options={})
0
       header = PREFIX.dup
0
       version = ActiveRecord::Migrator.current_version rescue 0
0
       if version > 0
0
@@ -123,7 +136,7 @@ module AnnotateModels
0
           klass = get_model_class(file)
0
           if klass < ActiveRecord::Base && !klass.abstract_class?
0
             annotated << klass
0
- annotate(klass, file, header)
0
+ annotate(klass, file, header,options)
0
           end
0
         rescue Exception => e
0
           puts "Unable to annotate #{file}: #{e.message}"
0
@@ -131,5 +144,28 @@ module AnnotateModels
0
       end
0
       puts "Annotated #{annotated.join(', ')}"
0
     end
0
+
0
+ def remove_annotations
0
+ deannotated = []
0
+ get_model_files.each do |file|
0
+ begin
0
+ klass = get_model_class(file)
0
+ if klass < ActiveRecord::Base && !klass.abstract_class?
0
+ deannotated << klass
0
+
0
+ model_file_name = File.join(MODEL_DIR, file)
0
+ remove_annotation_of_file(model_file_name)
0
+
0
+ FIXTURE_DIRS.each do |dir|
0
+ fixture_file_name = File.join(dir,klass.table_name + ".yml")
0
+ remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name)
0
+ end
0
+ end
0
+ rescue Exception => e
0
+ puts "Unable to annotate #{file}: #{e.message}"
0
+ end
0
+ end
0
+ puts "Removed annotaion from: #{deannotated.join(', ')}"
0
+ end
0
   end
0
 end
0
\ No newline at end of file
...
1
 
2
3
4
 
 
 
 
 
 
 
 
 
 
5
6
...
 
1
2
3
 
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -1,5 +1,14 @@
0
-desc "Add schema information (as comments) to model files"
0
+desc "Add schema information (as comments) to model and fixture files"
0
 task :annotate_models => :environment do
0
   require 'annotate_models'
0
- AnnotateModels.do_annotations
0
+ options={}
0
+ options[:position_in_class] = ENV['position_in_class'] || ENV['position']
0
+ options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position']
0
+ AnnotateModels.do_annotations(options)
0
+end
0
+
0
+desc "Remove schema information from model and fixture files"
0
+task :remove_annotation => :environment do
0
+ require 'annotate_models'
0
+ AnnotateModels.remove_annotations
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.