@@ -31,8 +31,8 @@ module AnnotateModels
3131 FABRICATORS_SPEC_DIR = File . join ( "spec" , "fabricators" )
3232
3333 TEST_PATTERNS = [
34- [ UNIT_TEST_DIR , "%MODEL_NAME%_test.rb" ] ,
35- [ SPEC_MODEL_DIR , "%MODEL_NAME%_spec.rb" ] ,
34+ File . join ( UNIT_TEST_DIR , "%MODEL_NAME%_test.rb" ) ,
35+ File . join ( SPEC_MODEL_DIR , "%MODEL_NAME%_spec.rb" ) ,
3636 ]
3737
3838 FIXTURE_PATTERNS = [
@@ -131,6 +131,11 @@ def get_schema_info(klass, header, options = {})
131131 end
132132 end
133133
134+ # Check out if we got an array column
135+ if col . respond_to? ( :array ) && col . array
136+ attrs << "is an Array"
137+ end
138+
134139 # Check out if we got a geometric column
135140 # and print the type and SRID
136141 if col . respond_to? ( :geometry_type )
@@ -227,27 +232,23 @@ def annotate_one_file(file_name, info_block, position, options={})
227232 if old_columns == new_columns && !options [ :force ]
228233 return false
229234 else
235+ # Replace inline the old schema info with the new schema info
236+ new_content = old_content . sub ( PATTERN , info_block + "\n " )
230237
231- # todo: figure out if we need to extract any logic from this merge chunk
232- # <<<<<<< HEAD
233- # # Replace the old schema info with the new schema info
234- # new_content = old_content.sub(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n*/, info_block)
235- # # But, if there *was* no old schema info, we simply need to insert it
236- # if new_content == old_content
237- # old_content.sub!(encoding, '')
238- # new_content = options[:position] == 'after' ?
239- # (encoding_header + (old_content =~ /\n$/ ? old_content : old_content + "\n") + info_block) :
240- # (encoding_header + info_block + old_content)
241- # end
242- # =======
243-
244- # Strip the old schema info, and insert new schema info.
245- old_content . sub! ( encoding , '' )
246- old_content . sub! ( PATTERN , '' )
247-
248- new_content = options [ position ] . to_s == 'after' ?
249- ( encoding_header + ( old_content . rstrip + "\n \n " + info_block ) ) :
250- ( encoding_header + info_block + "\n " + old_content )
238+ if new_content . end_with? ( info_block + "\n " )
239+ new_content = old_content . sub ( PATTERN , "\n " + info_block )
240+ end
241+
242+ # if there *was* no old schema info (no substitution happened) or :force was passed,
243+ # we simply need to insert it in correct position
244+ if new_content == old_content || options [ :force ]
245+ old_content . sub! ( encoding , '' )
246+ old_content . sub! ( PATTERN , '' )
247+
248+ new_content = options [ position ] . to_s == 'after' ?
249+ ( encoding_header + ( old_content . rstrip + "\n \n " + info_block ) ) :
250+ ( encoding_header + info_block + "\n " + old_content )
251+ end
251252
252253 File . open ( file_name , "wb" ) { |f | f . puts new_content }
253254 return true
@@ -301,8 +302,7 @@ def annotate(klass, file, header, options={})
301302
302303 unless options [ :exclude_tests ]
303304 did_annotate = TEST_PATTERNS .
304- map { |pat | [ pat [ 0 ] , resolve_filename ( pat [ 1 ] , model_name , table_name ) ] } .
305- map { |pat | find_test_file ( *pat ) } .
305+ map { |file | resolve_filename ( file , model_name , table_name ) } .
306306 map { |file | annotate_one_file ( file , info , :position_in_test , options_with_position ( options , :position_in_test ) ) } .
307307 detect { |result | result } || did_annotate
308308 end
@@ -370,7 +370,7 @@ def get_model_files(options)
370370 # in subdirectories without namespacing.
371371 def get_model_class ( file )
372372 # this is for non-rails projects, which don't get Rails auto-require magic
373- require File . expand_path ( "#{ model_dir } /#{ file } " ) unless Module . const_defined? ( :Rails )
373+ require File . expand_path ( "#{ model_dir } /#{ file } " )
374374 model_path = file . gsub ( /\. rb$/ , '' )
375375 get_loaded_model ( model_path ) || get_loaded_model ( model_path . split ( '/' ) . last )
376376 end
@@ -441,16 +441,7 @@ def remove_annotations(options={})
441441 model_file_name = File . join ( model_dir , file )
442442 deannotated_klass = true if ( remove_annotation_of_file ( model_file_name ) )
443443
444- TEST_PATTERNS .
445- map { |pat | [ pat [ 0 ] , resolve_filename ( pat [ 1 ] , model_name , table_name ) ] } .
446- map { |pat | find_test_file ( *pat ) } . each do |file |
447- if ( File . exist? ( file ) )
448- remove_annotation_of_file ( file )
449- deannotated_klass = true
450- end
451- end
452-
453- ( FIXTURE_PATTERNS + FACTORY_PATTERNS ) .
444+ ( TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS ) .
454445 map { |file | resolve_filename ( file , model_name , table_name ) } .
455446 each do |file |
456447 if File . exist? ( file )
@@ -468,10 +459,6 @@ def remove_annotations(options={})
468459 puts "Removed annotations from: #{ deannotated . join ( ', ' ) } "
469460 end
470461
471- def find_test_file ( dir , file_name )
472- Dir . glob ( File . join ( dir , "**" , file_name ) ) . first || File . join ( dir , file_name )
473- end
474-
475462 def resolve_filename ( filename_template , model_name , table_name )
476463 return filename_template .
477464 gsub ( '%MODEL_NAME%' , model_name ) .
0 commit comments