From b11928ed0d787e36949520345edd9e80007ec767 Mon Sep 17 00:00:00 2001 From: Marcin Raczkowski Date: Mon, 17 Feb 2014 11:51:11 +0100 Subject: [PATCH] Replacing old block with new one. --- lib/annotate/annotate_models.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index e50275e2e..dfae6b823 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -241,13 +241,17 @@ def annotate_one_file(file_name, info_block, position, options={}) # end # ======= - # Strip the old schema info, and insert new schema info. - old_content.sub!(encoding, '') - old_content.sub!(PATTERN, '') - - new_content = options[position].to_s == 'after' ? - (encoding_header + (old_content.rstrip + "\n\n" + info_block)) : - (encoding_header + info_block + "\n" + old_content) + if PATTERN.match(old_content) + new_content = old_content.sub(PATTERN, info_block) + else + # Strip the old schema info, and insert new schema info. + old_content.sub!(encoding, '') + old_content.sub!(PATTERN, '') + + new_content = options[position].to_s == 'after' ? + (encoding_header + (old_content.rstrip + "\n\n" + info_block)) : + (encoding_header + info_block + "\n" + old_content) + end File.open(file_name, "wb") { |f| f.puts new_content } return true