1
1
require "config/environment"
2
2
3
- MODEL_DIR = File . join ( RAILS_ROOT , "app/models" )
3
+ MODEL_DIR = File . join ( RAILS_ROOT , "app/models" )
4
4
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
7
8
8
9
module AnnotateModels
9
10
@@ -43,22 +44,22 @@ def self.get_schema_info(klass, header)
43
44
klass . columns
44
45
end . each { |col | info << annotate_column ( col , klass , max_size ) }
45
46
46
- info << "# \n \n "
47
+ info << "\n "
47
48
end
48
49
49
50
def self . annotate_column ( col , klass , max_size )
50
51
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
53
54
attrs << "primary key" if col . name == klass . primary_key
54
-
55
+
55
56
col_type = col . type . to_s
56
57
if col_type == "decimal"
57
58
col_type << "(#{ col . precision } , #{ col . scale } )"
58
59
else
59
60
col_type << "(#{ col . limit } )" if col . limit
60
61
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 "
62
63
end
63
64
64
65
# 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)
75
76
76
77
# Write it back
77
78
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
79
80
end
80
81
end
81
82
end
@@ -88,14 +89,12 @@ def self.annotate_one_file(file_name, info_block)
88
89
def self . annotate ( klass , header )
89
90
info = get_schema_info ( klass , header )
90
91
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 ) }
99
98
end
100
99
101
100
# Return a list of the model files to annotate. If we have
0 commit comments