public
Fork of ctran/annotate_models
Description: Annotate ActiveRecord models
Homepage: http://agilewebdevelopment.com/plugins/annotate_models
Clone URL: git://github.com/JackDanger/annotate_models.git
Search Repo:
ctran (author)
Wed Feb 27 10:06:49 -0800 2008
commit  1da0386bf9e1ca3fbd0d9d3ae69cdc7a8cdc26fa
tree    bd126620d7889c46e260cf07d3bfc98ecb4f577f
...
 
 
 
 
...
1
2
3
4
0
@@ -0,0 +1,4 @@
0
+== 0.0.1 2008-02-27
0
+
0
+* 1 major enhancement:
0
+ * Initial release
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0
@@ -0,0 +1,20 @@
0
+Copyright (c) 2008 FIXME full name
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+"Software"), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -0,0 +1,24 @@
0
+History.txt
0
+License.txt
0
+Manifest.txt
0
+README.txt
0
+Rakefile
0
+config/hoe.rb
0
+config/requirements.rb
0
+lib/annotate_models.rb
0
+lib/annotate_models/version.rb
0
+log/debug.log
0
+script/destroy
0
+script/generate
0
+script/txt2html
0
+setup.rb
0
+tasks/deployment.rake
0
+tasks/environment.rake
0
+tasks/website.rake
0
+test/test_annotate_models.rb
0
+test/test_helper.rb
0
+website/index.html
0
+website/index.txt
0
+website/javascripts/rounded_corners_lite.inc.js
0
+website/stylesheets/screen.css
0
+website/template.rhtml
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
0
@@ -0,0 +1,32 @@
0
+AnnotateModels
0
+==============
0
+
0
+Add a comment summarizing the current schema to the top of each ActiveRecord model source file.
0
+
0
+ # Schema as of Sun Feb 26 21:58:32 CST 2006 (schema version 7)
0
+ #
0
+ # id :integer(11) not null
0
+ # quantity :integer(11)
0
+ # product_id :integer(11)
0
+ # unit_price :float
0
+ # order_id :integer(11)
0
+ #
0
+
0
+ class LineItem < ActiveRecord::Base
0
+ belongs_to :product
0
+
0
+ end
0
+
0
+Note that this code will blow away the initial comment block in your models if it looks like it was
0
+previously added by annotate models, so you don't want to add additional text to an automatically
0
+created comment block.
0
+
0
+Author:
0
+ Dave Thomas
0
+ Pragmatic Programmers, LLC
0
+
0
+Released under the same license as Ruby. No Support. No Warranty.
0
+
0
+Modifications by:
0
+ - alex@pivotallabs.com
0
+ - ctran@pragmaquest.com
...
 
 
 
 
0
...
1
2
3
4
5
0
@@ -0,0 +1,4 @@
0
+require 'config/requirements'
0
+require 'config/hoe' # setup Hoe + all gem configuration
0
+
0
+Dir['tasks/**/*.rake'].each { |rake| load rake }
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
0
@@ -0,0 +1,70 @@
0
+require 'annotate_models/version'
0
+
0
+AUTHOR = 'Dave Thomas' # can also be an array of Authors
0
+EMAIL = "ctran@pragmaquest.com"
0
+DESCRIPTION = "Add a comment summarizing the current schema to the top of each ActiveRecord model source file"
0
+GEM_NAME = 'annotate_models' # what ppl will type to install your gem
0
+RUBYFORGE_PROJECT = 'annotate-models' # The unix name for your project
0
+HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
0
+DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
0
+
0
+@config_file = "~/.rubyforge/user-config.yml"
0
+@config = nil
0
+RUBYFORGE_USERNAME = "unknown"
0
+def rubyforge_username
0
+ unless @config
0
+ begin
0
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
0
+ rescue
0
+ puts <<-EOS
0
+ERROR: No rubyforge config file found: #{@config_file}
0
+Run 'rubyforge setup' to prepare your env for access to Rubyforge
0
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
0
+ EOS
0
+ exit
0
+ end
0
+ end
0
+ RUBYFORGE_USERNAME.replace @config["username"]
0
+end
0
+
0
+
0
+REV = nil
0
+# UNCOMMENT IF REQUIRED:
0
+# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
0
+VERS = AnnotateModels::VERSION::STRING + (REV ? ".#{REV}" : "")
0
+RDOC_OPTS = ['--quiet', '--title', 'annotate_models documentation',
0
+ "--opname", "index.html",
0
+ "--line-numbers",
0
+ "--main", "README",
0
+ "--inline-source"]
0
+
0
+class Hoe
0
+ def extra_deps
0
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
0
+ @extra_deps
0
+ end
0
+end
0
+
0
+# Generate all the Rake tasks
0
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
0
+hoe = Hoe.new(GEM_NAME, VERS) do |p|
0
+ p.developer(AUTHOR, EMAIL)
0
+ p.description = DESCRIPTION
0
+ p.summary = DESCRIPTION
0
+ p.url = HOMEPATH
0
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
0
+ p.test_globs = ["test/**/test_*.rb"]
0
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
0
+
0
+ # == Optional
0
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
0
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
0
+
0
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
0
+
0
+end
0
+
0
+CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
0
+PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
0
+hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
0
+hoe.rsync_args = '-av --delete --ignore-errors'
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
0
@@ -0,0 +1,17 @@
0
+require 'fileutils'
0
+include FileUtils
0
+
0
+require 'rubygems'
0
+%w[rake hoe newgem rubigen].each do |req_gem|
0
+ begin
0
+ require req_gem
0
+ rescue LoadError
0
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
0
+ puts "Installation: gem install #{req_gem} -y"
0
+ exit
0
+ end
0
+end
0
+
0
+$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
0
+
0
+require 'annotate_models'
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
0
@@ -0,0 +1,126 @@
0
+$:.unshift File.dirname(__FILE__)
0
+
0
+module AnnotateModels
0
+ RAILS_ROOT = '.'
0
+ MODEL_DIR = File.join(RAILS_ROOT, "app/models")
0
+ FIXTURE_DIR = File.join(RAILS_ROOT, "test/fixtures")
0
+ PREFIX = "== Schema Information"
0
+
0
+ # Simple quoting for the default column value
0
+ def self.quote(value)
0
+ case value
0
+ when NilClass then "NULL"
0
+ when TrueClass then "TRUE"
0
+ when FalseClass then "FALSE"
0
+ when Float, Fixnum, Bignum then value.to_s
0
+ # BigDecimals need to be output in a non-normalized form and quoted.
0
+ when BigDecimal then value.to_s('F')
0
+ else
0
+ value.inspect
0
+ end
0
+ end
0
+
0
+ # Use the column information in an ActiveRecord class
0
+ # to create a comment block containing a line for
0
+ # each column. The line contains the column name,
0
+ # the type (and length), and any optional attributes
0
+ def self.get_schema_info(klass, header)
0
+ info = "# #{header}\n#\n"
0
+ info << "# Table name: #{klass.table_name}\n#\n"
0
+
0
+ max_size = klass.column_names.collect{|name| name.size}.max + 1
0
+ klass.columns.each do |col|
0
+ attrs = []
0
+ attrs << "default(#{quote(col.default)})" if col.default
0
+ attrs << "not null" unless col.null
0
+ attrs << "primary key" if col.name == klass.primary_key
0
+
0
+ col_type = col.type.to_s
0
+ if col_type == "decimal"
0
+ col_type << "(#{col.precision}, #{col.scale})"
0
+ else
0
+ col_type << "(#{col.limit})" if col.limit
0
+ end
0
+ info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s\n", col.name, col_type, attrs.join(", "))
0
+ end
0
+
0
+ info << "#\n\n"
0
+ end
0
+
0
+ # Add a schema block to a file. If the file already contains
0
+ # a schema info block (a comment starting
0
+ # with "Schema as of ..."), remove it first.
0
+
0
+ def self.annotate_one_file(file_name, info_block)
0
+ if File.exist?(file_name)
0
+ content = File.read(file_name)
0
+
0
+ # Remove old schema info
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
+ end
0
+ end
0
+
0
+ # Given the name of an ActiveRecord class, create a schema
0
+ # info block (basically a comment containing information
0
+ # on the columns and their types) and put it at the front
0
+ # of the model and fixture source files.
0
+
0
+ def self.annotate(klass, header)
0
+ info = get_schema_info(klass, header)
0
+
0
+ model_file_name = File.join(MODEL_DIR, klass.name.underscore + ".rb")
0
+ annotate_one_file(model_file_name, info)
0
+
0
+ fixture_file_name = File.join(FIXTURE_DIR, klass.table_name + ".yml")
0
+ annotate_one_file(fixture_file_name, info)
0
+ end
0
+
0
+ # Return a list of the model files to annotate. If we have
0
+ # command line arguments, they're assumed to be either
0
+ # the underscore or CamelCase versions of model names.
0
+ # Otherwise we take all the model files in the
0
+ # app/models directory.
0
+ def self.get_model_names
0
+ models = ARGV.dup
0
+ models.shift
0
+
0
+ if models.empty?
0
+ Dir.chdir(MODEL_DIR) do
0
+ models = Dir["**/*.rb"]
0
+ end
0
+ end
0
+ models
0
+ end
0
+
0
+ # We're passed a name of things that might be
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
+
0
+ def self.do_annotations
0
+ header = PREFIX.dup
0
+ version = ActiveRecord::Migrator.current_version rescue 0
0
+ if version > 0
0
+ header << "\n# Schema version: #{version}"
0
+ end
0
+
0
+ annotated = []
0
+ self.get_model_names.each do |m|
0
+ class_name = m.sub(/\.rb$/,'').camelize
0
+ begin
0
+ klass = class_name.split('::').inject(Object){ |klass,part| klass.const_get(part) }
0
+ if klass < ActiveRecord::Base && !klass.abstract_class?
0
+ annotated << class_name
0
+ self.annotate(klass, header)
0
+ end
0
+ rescue Exception => e
0
+ puts "Unable to annotate #{class_name}: #{e.message}"
0
+ end
0
+
0
+ end
0
+ puts "Annotated #{annotated.join(', ')}"
0
+ end
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
0
@@ -0,0 +1,9 @@
0
+module AnnotateModels #:nodoc:
0
+ module VERSION #:nodoc:
0
+ MAJOR = 0
0
+ MINOR = 0
0
+ TINY = 1
0
+
0
+ STRING = [MAJOR, MINOR, TINY].join('.')
0
+ end
0
+end
...
 
 
 
 
...
1
2
3
4
0
@@ -0,0 +1,4 @@
0
+== 0.0.1 2008-02-27
0
+
0
+* 1 major enhancement:
0
+ * Initial release
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0
@@ -0,0 +1,20 @@
0
+Copyright (c) 2008 FIXME full name
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+"Software"), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -0,0 +1,24 @@
0
+History.txt
0
+License.txt
0
+Manifest.txt
0
+README.txt
0
+Rakefile
0
+config/hoe.rb
0
+config/requirements.rb
0
+lib/annotate_models.rb
0
+lib/annotate_models/version.rb
0
+log/debug.log
0
+script/destroy
0
+script/generate
0
+script/txt2html
0
+setup.rb
0
+tasks/deployment.rake
0
+tasks/environment.rake
0
+tasks/website.rake
0
+test/test_annotate_models.rb
0
+test/test_helper.rb
0
+website/index.html
0
+website/index.txt
0
+website/javascripts/rounded_corners_lite.inc.js
0
+website/stylesheets/screen.css
0
+website/template.rhtml
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
0
@@ -0,0 +1,32 @@
0
+AnnotateModels
0
+==============
0
+
0
+Add a comment summarizing the current schema to the top of each ActiveRecord model source file.
0
+
0
+ # Schema as of Sun Feb 26 21:58:32 CST 2006 (schema version 7)
0
+ #
0
+ # id :integer(11) not null
0
+ # quantity :integer(11)
0
+ # product_id :integer(11)
0
+ # unit_price :float
0
+ # order_id :integer(11)
0
+ #
0
+
0
+ class LineItem < ActiveRecord::Base
0
+ belongs_to :product
0
+
0
+ end
0
+
0
+Note that this code will blow away the initial comment block in your models if it looks like it was
0
+previously added by annotate models, so you don't want to add additional text to an automatically
0
+created comment block.
0
+
0
+Author:
0
+ Dave Thomas
0
+ Pragmatic Programmers, LLC
0
+
0
+Released under the same license as Ruby. No Support. No Warranty.
0
+
0
+Modifications by:
0
+ - alex@pivotallabs.com
0
+ - ctran@pragmaquest.com
...
 
 
 
 
0
...
1
2
3
4
5
0
@@ -0,0 +1,4 @@
0
+require 'config/requirements'
0
+require 'config/hoe' # setup Hoe + all gem configuration
0
+
0
+Dir['tasks/**/*.rake'].each { |rake| load rake }
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
0
@@ -0,0 +1,70 @@
0
+require 'annotate_models/version'
0
+
0
+AUTHOR = 'Dave Thomas' # can also be an array of Authors
0
+EMAIL = "ctran@pragmaquest.com"
0
+DESCRIPTION = "Add a comment summarizing the current schema to the top of each ActiveRecord model source file"
0
+GEM_NAME = 'annotate_models' # what ppl will type to install your gem
0
+RUBYFORGE_PROJECT = 'annotate-models' # The unix name for your project
0
+HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
0
+DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
0
+
0
+@config_file = "~/.rubyforge/user-config.yml"
0
+@config = nil
0
+RUBYFORGE_USERNAME = "unknown"
0
+def rubyforge_username
0
+ unless @config
0
+ begin
0
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
0
+ rescue
0
+ puts <<-EOS
0
+ERROR: No rubyforge config file found: #{@config_file}
0
+Run 'rubyforge setup' to prepare your env for access to Rubyforge
0
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
0
+ EOS
0
+ exit
0
+ end
0
+ end
0
+ RUBYFORGE_USERNAME.replace @config["username"]
0
+end
0
+
0
+
0
+REV = nil
0
+# UNCOMMENT IF REQUIRED:
0
+# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
0
+VERS = AnnotateModels::VERSION::STRING + (REV ? ".#{REV}" : "")
0
+RDOC_OPTS = ['--quiet', '--title', 'annotate_models documentation',
0
+ "--opname", "index.html",
0
+ "--line-numbers",
0
+ "--main", "README",
0
+ "--inline-source"]
0
+
0
+class Hoe
0
+ def extra_deps
0
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
0
+ @extra_deps
0
+ end
0
+end
0
+
0
+# Generate all the Rake tasks
0
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
0
+hoe = Hoe.new(GEM_NAME, VERS) do |p|
0
+ p.developer(AUTHOR, EMAIL)
0
+ p.description = DESCRIPTION
0
+ p.summary = DESCRIPTION
0
+ p.url = HOMEPATH
0
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
0
+ p.test_globs = ["test/**/test_*.rb"]
0
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
0
+
0
+ # == Optional
0
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
0
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
0
+
0
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
0
+
0
+end
0
+
0
+CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
0
+PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
0
+hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
0
+hoe.rsync_args = '-av --delete --ignore-errors'
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
0
@@ -0,0 +1,17 @@
0
+require 'fileutils'
0
+include FileUtils
0
+
0
+require 'rubygems'
0
+%w[rake hoe newgem rubigen].each do |req_gem|
0
+ begin
0
+ require req_gem
0
+ rescue LoadError
0
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
0
+ puts "Installation: gem install #{req_gem} -y"
0
+ exit
0
+ end
0
+end
0
+
0
+$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
0
+
0
+require 'annotate_models'
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
0
@@ -0,0 +1,126 @@
0
+$:.unshift File.dirname(__FILE__)
0
+
0
+module AnnotateModels
0
+ RAILS_ROOT = '.'
0
+ MODEL_DIR = File.join(RAILS_ROOT, "app/models")
0
+ FIXTURE_DIR = File.join(RAILS_ROOT, "test/fixtures")
0
+ PREFIX = "== Schema Information"
0
+
0
+ # Simple quoting for the default column value
0
+ def self.quote(value)
0
+ case value
0
+ when NilClass then "NULL"
0
+ when TrueClass then "TRUE"
0
+ when FalseClass then "FALSE"
0
+ when Float, Fixnum, Bignum then value.to_s
0
+ # BigDecimals need to be output in a non-normalized form and quoted.
0
+ when BigDecimal then value.to_s('F')
0
+ else
0
+ value.inspect
0
+ end
0
+ end
0
+
0
+ # Use the column information in an ActiveRecord class
0
+ # to create a comment block containing a line for
0
+ # each column. The line contains the column name,
0
+ # the type (and length), and any optional attributes
0
+ def self.get_schema_info(klass, header)
0
+ info = "# #{header}\n#\n"
0
+ info << "# Table name: #{klass.table_name}\n#\n"
0
+
0
+ max_size = klass.column_names.collect{|name| name.size}.max + 1
0
+ klass.columns.each do |col|
0
+ attrs = []
0
+ attrs << "default(#{quote(col.default)})" if col.default
0
+ attrs << "not null" unless col.null
0
+ attrs << "primary key" if col.name == klass.primary_key
0
+
0
+ col_type = col.type.to_s
0
+ if col_type == "decimal"
0
+ col_type << "(#{col.precision}, #{col.scale})"
0
+ else
0
+ col_type << "(#{col.limit})" if col.limit
0
+ end
0
+ info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s\n", col.name, col_type, attrs.join(", "))
0
+ end
0
+
0
+ info << "#\n\n"
0
+ end
0
+
0
+ # Add a schema block to a file. If the file already contains
0
+ # a schema info block (a comment starting
0
+ # with "Schema as of ..."), remove it first.
0
+
0
+ def self.annotate_one_file(file_name, info_block)
0
+ if File.exist?(file_name)
0
+ content = File.read(file_name)
0
+
0
+ # Remove old schema info
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
+ end
0
+ end
0
+
0
+ # Given the name of an ActiveRecord class, create a schema
0
+ # info block (basically a comment containing information
0
+ # on the columns and their types) and put it at the front
0
+ # of the model and fixture source files.
0
+
0
+ def self.annotate(klass, header)
0
+ info = get_schema_info(klass, header)
0
+
0
+ model_file_name = File.join(MODEL_DIR, klass.name.underscore + ".rb")
0
+ annotate_one_file(model_file_name, info)
0
+
0
+ fixture_file_name = File.join(FIXTURE_DIR, klass.table_name + ".yml")
0
+ annotate_one_file(fixture_file_name, info)
0
+ end
0
+
0
+ # Return a list of the model files to annotate. If we have
0
+ # command line arguments, they're assumed to be either
0
+ # the underscore or CamelCase versions of model names.
0
+ # Otherwise we take all the model files in the
0
+ # app/models directory.
0
+ def self.get_model_names
0
+ models = ARGV.dup
0
+ models.shift
0
+
0
+ if models.empty?
0
+ Dir.chdir(MODEL_DIR) do
0
+ models = Dir["**/*.rb"]
0
+ end
0
+ end
0
+ models
0
+ end
0
+
0
+ # We're passed a name of things that might be
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
+
0
+ def self.do_annotations
0
+ header = PREFIX.dup
0
+ version = ActiveRecord::Migrator.current_version rescue 0
0
+ if version > 0