Skip to content

fix so that the wrapper-opening does not gets duplicated #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Metrics/MethodLength:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 513
Max: 516

# Offense count: 7
Metrics/PerceivedComplexity:
Expand Down
16 changes: 11 additions & 5 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module AnnotateModels
PREFIX = "== Schema Information"
PREFIX_MD = "## Schema Information"
END_MARK = "== Schema Information End"
PATTERN = /^\r?\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\r?\n(#.*\r?\n)*(\r?\n)*/

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

Expand Down Expand Up @@ -65,6 +64,13 @@ module AnnotateModels
NO_DEFAULT_COL_TYPES = %w(json jsonb)

class << self
def annotate_pattern(options = {})
if options[:wrapper_open]
return /(?:^\n?# (?:#{options[:wrapper_open]}).*\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\n(#.*\n)*\n*)|^\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\n(#.*\n)*\n*/
end
/^\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\n(#.*\n)*\n*/
end

def model_dir
@model_dir.is_a?(Array) ? @model_dir : [@model_dir || 'app/models']
end
Expand Down Expand Up @@ -378,10 +384,10 @@ def annotate_one_file(file_name, info_block, position, options={})
return false
else
# Replace inline the old schema info with the new schema info
new_content = old_content.sub(PATTERN, info_block + "\n")
new_content = old_content.sub(annotate_pattern(options), info_block + "\n")

if new_content.end_with?(info_block + "\n")
new_content = old_content.sub(PATTERN, "\n" + info_block)
new_content = old_content.sub(annotate_pattern(options), "\n" + info_block)
end

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

if %w(after bottom).include?(options[position].to_s)
new_content = magic_comments.join + (old_content.rstrip + "\n\n" + wrapped_info_block)
Expand All @@ -412,7 +418,7 @@ def remove_annotation_of_file(file_name, options={})
if File.exist?(file_name)
content = File.read(file_name)
wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ""
content.sub!(/(#{wrapper_open})?#{PATTERN}/, '')
content.sub!(/(#{wrapper_open})?#{annotate_pattern(options)}/, '')

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

Expand Down