@@ -7,7 +7,7 @@ module AnnotateModels
77 PREFIX = "== Schema Information"
88 PREFIX_MD = "## Schema Information"
99 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 ) */
1111
1212 # File.join for windows reverse bar compat?
1313 # I dont use windows, can`t test
@@ -140,7 +140,7 @@ def get_schema_info(klass, header, options = {})
140140 attrs << "not null" unless col . null
141141 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 )
142142
143- col_type = ( col . type || col . sql_type ) . to_s
143+ col_type = ( col . sql_type || col . type ) . to_s
144144 if col_type == "decimal"
145145 col_type << "(#{ col . precision } , #{ col . scale } )"
146146 elsif col_type != "spatial"
@@ -193,6 +193,10 @@ def get_schema_info(klass, header, options = {})
193193 info << get_index_info ( klass , options )
194194 end
195195
196+ if options [ :show_foreign_keys ] && klass . table_exists?
197+ info << get_foreign_key_info ( klass , options )
198+ end
199+
196200 if options [ :format_rdoc ]
197201 info << "#--\n "
198202 info << "# #{ END_MARK } \n "
@@ -223,6 +227,28 @@ def get_index_info(klass, options={})
223227 return index_info
224228 end
225229
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+
226252 # Add a schema block to a file. If the file already contains
227253 # a schema info block (a comment starting with "== Schema Information"), check if it
228254 # 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)
309335 # :position_in_test<Symbol>:: where to place the annotated section in test/spec file(s)
310336 # :position_in_fixture<Symbol>:: where to place the annotated section in fixture file
311337 # :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
312339 # :exclude_tests<Symbol>:: whether to skip modification of test/spec files
313340 # :exclude_fixtures<Symbol>:: whether to skip modification of fixture files
314341 # :exclude_factories<Symbol>:: whether to skip modification of factory files
342+ # :exclude_serializers<Symbol>:: whether to skip modification of serializer files
315343 #
316344 def annotate ( klass , file , header , options = { } )
317345 begin
@@ -350,9 +378,9 @@ def options_with_position(options, position_in)
350378 options . merge ( :position => ( options [ position_in ] || options [ :position ] ) )
351379 end
352380
353- # Return a list of the model files to annotate.
381+ # Return a list of the model files to annotate.
354382 # 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
356384 # in the model_dir directory.
357385 def get_model_files ( options )
358386 models = [ ]
@@ -364,7 +392,7 @@ def get_model_files(options)
364392 begin
365393 model_dir . each do |dir |
366394 Dir . chdir ( dir ) do
367- lst =
395+ lst =
368396 if options [ :ignore_model_sub_dir ]
369397 Dir [ "*.rb" ] . map { |f | [ dir , f ] }
370398 else
@@ -451,6 +479,7 @@ def do_annotations(options={})
451479
452480 def annotate_model_file ( annotated , file , header , options )
453481 begin
482+ return false if ( /# -\* - SkipSchemaAnnotations.*/ =~ ( File . exist? ( file ) ? File . read ( file ) : '' ) )
454483 klass = get_model_class ( file )
455484 if klass && klass < ActiveRecord ::Base && !klass . abstract_class? && klass . table_exists?
456485 if annotate ( klass , file , header , options )
@@ -477,7 +506,7 @@ def remove_annotations(options={})
477506 model_file_name = file
478507 deannotated_klass = true if ( remove_annotation_of_file ( model_file_name ) )
479508
480- ( TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS ) .
509+ ( TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS + SERIALIZER_PATTERNS ) .
481510 map { |file | resolve_filename ( file , model_name , table_name ) } .
482511 each do |file |
483512 if File . exist? ( file )
0 commit comments