@@ -7,7 +7,7 @@ module AnnotateModels
7
7
PREFIX = "== Schema Information"
8
8
PREFIX_MD = "## Schema Information"
9
9
END_MARK = "== Schema Information End"
10
- PATTERN = /^\n ?# (?:#{ COMPAT_PREFIX } |#{ COMPAT_PREFIX_MD } ).*?\n (#.*\n )*\n */
10
+ PATTERN = /^\r ? \ n ?# (?:#{ COMPAT_PREFIX } |#{ COMPAT_PREFIX_MD } ).*?\r ? \ n (#.*\r ? \ n )*( \r ? \n ) */
11
11
12
12
# File.join for windows reverse bar compat?
13
13
# I dont use windows, can`t test
@@ -140,7 +140,7 @@ def get_schema_info(klass, header, options = {})
140
140
attrs << "not null" unless col . null
141
141
attrs << "primary key" if klass . primary_key && ( klass . primary_key . is_a? ( Array ) ? klass . primary_key . collect { |c |c . to_sym } . include? ( col . name . to_sym ) : col . name . to_sym == klass . primary_key . to_sym )
142
142
143
- col_type = ( col . type || col . sql_type ) . to_s
143
+ col_type = ( col . sql_type || col . type ) . to_s
144
144
if col_type == "decimal"
145
145
col_type << "(#{ col . precision } , #{ col . scale } )"
146
146
elsif col_type != "spatial"
@@ -193,6 +193,10 @@ def get_schema_info(klass, header, options = {})
193
193
info << get_index_info ( klass , options )
194
194
end
195
195
196
+ if options [ :show_foreign_keys ] && klass . table_exists?
197
+ info << get_foreign_key_info ( klass , options )
198
+ end
199
+
196
200
if options [ :format_rdoc ]
197
201
info << "#--\n "
198
202
info << "# #{ END_MARK } \n "
@@ -223,6 +227,28 @@ def get_index_info(klass, options={})
223
227
return index_info
224
228
end
225
229
230
+ def get_foreign_key_info ( klass , options = { } )
231
+ if ( options [ :format_markdown ] )
232
+ fk_info = "#\n # ### Foreign Keys\n #\n "
233
+ else
234
+ fk_info = "#\n # Foreign Keys\n #\n "
235
+ end
236
+
237
+ foreign_keys = klass . connection . respond_to? ( :foreign_keys ) ? klass . connection . foreign_keys ( klass . table_name ) : [ ]
238
+ return "" if foreign_keys . empty?
239
+
240
+ max_size = foreign_keys . collect { |fk | fk . name . size } . max + 1
241
+ foreign_keys . sort_by { |fk | fk . name } . each do |fk |
242
+ ref_info = "#{ fk . column } => #{ fk . to_table } .#{ fk . primary_key } "
243
+ if ( options [ :format_markdown ] )
244
+ fk_info << sprintf ( "# * `%s`:\n # * **`%s`**\n " , fk . name , ref_info )
245
+ else
246
+ fk_info << sprintf ( "# %-#{ max_size } .#{ max_size } s %s" , fk . name , "(#{ ref_info } )" ) . rstrip + "\n "
247
+ end
248
+ end
249
+ return fk_info
250
+ end
251
+
226
252
# Add a schema block to a file. If the file already contains
227
253
# a schema info block (a comment starting with "== Schema Information"), check if it
228
254
# matches the block that is already there. If so, leave it be. If not, remove the old
@@ -309,9 +335,11 @@ def remove_annotation_of_file(file_name)
309
335
# :position_in_test<Symbol>:: where to place the annotated section in test/spec file(s)
310
336
# :position_in_fixture<Symbol>:: where to place the annotated section in fixture file
311
337
# :position_in_factory<Symbol>:: where to place the annotated section in factory file
338
+ # :position_in_serializer<Symbol>:: where to place the annotated section in serializer file
312
339
# :exclude_tests<Symbol>:: whether to skip modification of test/spec files
313
340
# :exclude_fixtures<Symbol>:: whether to skip modification of fixture files
314
341
# :exclude_factories<Symbol>:: whether to skip modification of factory files
342
+ # :exclude_serializers<Symbol>:: whether to skip modification of serializer files
315
343
#
316
344
def annotate ( klass , file , header , options = { } )
317
345
begin
@@ -350,9 +378,9 @@ def options_with_position(options, position_in)
350
378
options . merge ( :position => ( options [ position_in ] || options [ :position ] ) )
351
379
end
352
380
353
- # Return a list of the model files to annotate.
381
+ # Return a list of the model files to annotate.
354
382
# If we have command line arguments, they're assumed to the path
355
- # of model files from root dir. Otherwise we take all the model files
383
+ # of model files from root dir. Otherwise we take all the model files
356
384
# in the model_dir directory.
357
385
def get_model_files ( options )
358
386
models = [ ]
@@ -364,7 +392,7 @@ def get_model_files(options)
364
392
begin
365
393
model_dir . each do |dir |
366
394
Dir . chdir ( dir ) do
367
- lst =
395
+ lst =
368
396
if options [ :ignore_model_sub_dir ]
369
397
Dir [ "*.rb" ] . map { |f | [ dir , f ] }
370
398
else
@@ -451,6 +479,7 @@ def do_annotations(options={})
451
479
452
480
def annotate_model_file ( annotated , file , header , options )
453
481
begin
482
+ return false if ( /# -\* - SkipSchemaAnnotations.*/ =~ ( File . exist? ( file ) ? File . read ( file ) : '' ) )
454
483
klass = get_model_class ( file )
455
484
if klass && klass < ActiveRecord ::Base && !klass . abstract_class? && klass . table_exists?
456
485
if annotate ( klass , file , header , options )
@@ -477,7 +506,7 @@ def remove_annotations(options={})
477
506
model_file_name = file
478
507
deannotated_klass = true if ( remove_annotation_of_file ( model_file_name ) )
479
508
480
- ( TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS ) .
509
+ ( TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS + SERIALIZER_PATTERNS ) .
481
510
map { |file | resolve_filename ( file , model_name , table_name ) } .
482
511
each do |file |
483
512
if File . exist? ( file )
0 commit comments