@@ -9,7 +9,6 @@ module AnnotateModels
9
9
PREFIX = "== Schema Information"
10
10
PREFIX_MD = "## Schema Information"
11
11
END_MARK = "== Schema Information End"
12
- PATTERN = /^\r ?\n ?# (?:#{ COMPAT_PREFIX } |#{ COMPAT_PREFIX_MD } ).*?\r ?\n (#.*\r ?\n )*(\r ?\n )*/
13
12
14
13
MATCHED_TYPES = %w( test fixture factory serializer scaffold controller helper )
15
14
@@ -65,6 +64,13 @@ module AnnotateModels
65
64
NO_DEFAULT_COL_TYPES = %w( json jsonb )
66
65
67
66
class << self
67
+ def annotate_pattern ( options = { } )
68
+ if options [ :wrapper_open ]
69
+ return /(?:^\n ?# (?:#{ options [ :wrapper_open ] } ).*\n ?# (?:#{ COMPAT_PREFIX } |#{ COMPAT_PREFIX_MD } ).*?\n (#.*\n )*\n *)|^\n ?# (?:#{ COMPAT_PREFIX } |#{ COMPAT_PREFIX_MD } ).*?\n (#.*\n )*\n */
70
+ end
71
+ /^\n ?# (?:#{ COMPAT_PREFIX } |#{ COMPAT_PREFIX_MD } ).*?\n (#.*\n )*\n */
72
+ end
73
+
68
74
def model_dir
69
75
@model_dir . is_a? ( Array ) ? @model_dir : [ @model_dir || 'app/models' ]
70
76
end
@@ -378,10 +384,10 @@ def annotate_one_file(file_name, info_block, position, options={})
378
384
return false
379
385
else
380
386
# Replace inline the old schema info with the new schema info
381
- new_content = old_content . sub ( PATTERN , info_block + "\n " )
387
+ new_content = old_content . sub ( annotate_pattern ( options ) , info_block + "\n " )
382
388
383
389
if new_content . end_with? ( info_block + "\n " )
384
- new_content = old_content . sub ( PATTERN , "\n " + info_block )
390
+ new_content = old_content . sub ( annotate_pattern ( options ) , "\n " + info_block )
385
391
end
386
392
387
393
wrapper_open = options [ :wrapper_open ] ? "# #{ options [ :wrapper_open ] } \n " : ""
@@ -391,7 +397,7 @@ def annotate_one_file(file_name, info_block, position, options={})
391
397
# we simply need to insert it in correct position
392
398
if new_content == old_content || options [ :force ]
393
399
old_content . sub! ( magic_comment_matcher , '' )
394
- old_content . sub! ( PATTERN , '' )
400
+ old_content . sub! ( annotate_pattern ( options ) , '' )
395
401
396
402
if %w( after bottom ) . include? ( options [ position ] . to_s )
397
403
new_content = magic_comments . join + ( old_content . rstrip + "\n \n " + wrapped_info_block )
@@ -412,7 +418,7 @@ def remove_annotation_of_file(file_name, options={})
412
418
if File . exist? ( file_name )
413
419
content = File . read ( file_name )
414
420
wrapper_open = options [ :wrapper_open ] ? "# #{ options [ :wrapper_open ] } \n " : ""
415
- content . sub! ( /(#{ wrapper_open } )?#{ PATTERN } / , '' )
421
+ content . sub! ( /(#{ wrapper_open } )?#{ annotate_pattern ( options ) } / , '' )
416
422
417
423
File . open ( file_name , 'wb' ) { |f | f . puts content }
418
424
0 commit comments