@@ -31,8 +31,8 @@ module AnnotateModels
31
31
FABRICATORS_SPEC_DIR = File . join ( "spec" , "fabricators" )
32
32
33
33
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" ) ,
36
36
]
37
37
38
38
FIXTURE_PATTERNS = [
@@ -131,6 +131,11 @@ def get_schema_info(klass, header, options = {})
131
131
end
132
132
end
133
133
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
+
134
139
# Check out if we got a geometric column
135
140
# and print the type and SRID
136
141
if col . respond_to? ( :geometry_type )
@@ -227,27 +232,23 @@ def annotate_one_file(file_name, info_block, position, options={})
227
232
if old_columns == new_columns && !options [ :force ]
228
233
return false
229
234
else
235
+ # Replace inline the old schema info with the new schema info
236
+ new_content = old_content . sub ( PATTERN , info_block + "\n " )
230
237
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
251
252
252
253
File . open ( file_name , "wb" ) { |f | f . puts new_content }
253
254
return true
@@ -301,8 +302,7 @@ def annotate(klass, file, header, options={})
301
302
302
303
unless options [ :exclude_tests ]
303
304
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 ) } .
306
306
map { |file | annotate_one_file ( file , info , :position_in_test , options_with_position ( options , :position_in_test ) ) } .
307
307
detect { |result | result } || did_annotate
308
308
end
@@ -370,7 +370,7 @@ def get_model_files(options)
370
370
# in subdirectories without namespacing.
371
371
def get_model_class ( file )
372
372
# 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 } " )
374
374
model_path = file . gsub ( /\. rb$/ , '' )
375
375
get_loaded_model ( model_path ) || get_loaded_model ( model_path . split ( '/' ) . last )
376
376
end
@@ -441,16 +441,7 @@ def remove_annotations(options={})
441
441
model_file_name = File . join ( model_dir , file )
442
442
deannotated_klass = true if ( remove_annotation_of_file ( model_file_name ) )
443
443
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 ) .
454
445
map { |file | resolve_filename ( file , model_name , table_name ) } .
455
446
each do |file |
456
447
if File . exist? ( file )
@@ -468,10 +459,6 @@ def remove_annotations(options={})
468
459
puts "Removed annotations from: #{ deannotated . join ( ', ' ) } "
469
460
end
470
461
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
-
475
462
def resolve_filename ( filename_template , model_name , table_name )
476
463
return filename_template .
477
464
gsub ( '%MODEL_NAME%' , model_name ) .
0 commit comments