Skip to content

Commit e30044e

Browse files
fix so that the wrapper-opening does not gets duplicated
1 parent ec05bf6 commit e30044e

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Metrics/MethodLength:
105105
# Offense count: 2
106106
# Configuration parameters: CountComments.
107107
Metrics/ModuleLength:
108-
Max: 513
108+
Max: 516
109109

110110
# Offense count: 7
111111
Metrics/PerceivedComplexity:

lib/annotate/annotate_models.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module AnnotateModels
99
PREFIX = "== Schema Information"
1010
PREFIX_MD = "## Schema Information"
1111
END_MARK = "== Schema Information End"
12-
PATTERN = /^\r?\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\r?\n(#.*\r?\n)*(\r?\n)*/
1312

1413
MATCHED_TYPES = %w(test fixture factory serializer scaffold controller helper)
1514

@@ -65,6 +64,13 @@ module AnnotateModels
6564
NO_DEFAULT_COL_TYPES = %w(json jsonb)
6665

6766
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+
6874
def model_dir
6975
@model_dir.is_a?(Array) ? @model_dir : [@model_dir || 'app/models']
7076
end
@@ -378,10 +384,10 @@ def annotate_one_file(file_name, info_block, position, options={})
378384
return false
379385
else
380386
# 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")
382388

383389
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)
385391
end
386392

387393
wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ""
@@ -391,7 +397,7 @@ def annotate_one_file(file_name, info_block, position, options={})
391397
# we simply need to insert it in correct position
392398
if new_content == old_content || options[:force]
393399
old_content.sub!(magic_comment_matcher, '')
394-
old_content.sub!(PATTERN, '')
400+
old_content.sub!(annotate_pattern(options), '')
395401

396402
if %w(after bottom).include?(options[position].to_s)
397403
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={})
412418
if File.exist?(file_name)
413419
content = File.read(file_name)
414420
wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ""
415-
content.sub!(/(#{wrapper_open})?#{PATTERN}/, '')
421+
content.sub!(/(#{wrapper_open})?#{annotate_pattern(options)}/, '')
416422

417423
File.open(file_name, 'wb') { |f| f.puts content }
418424

0 commit comments

Comments
 (0)