Skip to content

Commit 992ad11

Browse files
sashabelozerovctran
authored andcommitted
[Fix #464] Add an empty line between magic comment and schema (#491)
1 parent 1e2047d commit 992ad11

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

lib/annotate/annotate_models.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,7 @@ def annotate_one_file(file_name, info_block, position, options = {})
503503
old_columns = old_header && old_header.scan(column_pattern).sort
504504
new_columns = new_header && new_header.scan(column_pattern).sort
505505

506-
magic_comment_matcher = Regexp.new(/(^#\s*encoding:.*\n)|(^# coding:.*\n)|(^# -\*- coding:.*\n)|(^# -\*- encoding\s?:.*\n)|(^#\s*frozen_string_literal:.+\n)|(^# -\*- frozen_string_literal\s*:.+-\*-\n)/)
507-
magic_comments = old_content.scan(magic_comment_matcher).flatten.compact
506+
magic_comments_block = magic_comments_as_string(old_content)
508507

509508
if old_columns == new_columns && !options[:force]
510509
return false
@@ -522,13 +521,13 @@ def annotate_one_file(file_name, info_block, position, options = {})
522521
# if there *was* no old schema info (no substitution happened) or :force was passed,
523522
# we simply need to insert it in correct position
524523
if new_content == old_content || options[:force]
525-
old_content.sub!(magic_comment_matcher, '')
524+
old_content.gsub!(magic_comment_matcher, '')
526525
old_content.sub!(annotate_pattern(options), '')
527526

528527
new_content = if %w(after bottom).include?(options[position].to_s)
529-
magic_comments.join + (old_content.rstrip + "\n\n" + wrapped_info_block)
528+
magic_comments_block + (old_content.rstrip + "\n\n" + wrapped_info_block)
530529
else
531-
magic_comments.join + wrapped_info_block + "\n" + old_content
530+
magic_comments_block + wrapped_info_block + "\n" + old_content
532531
end
533532
end
534533

@@ -540,6 +539,20 @@ def annotate_one_file(file_name, info_block, position, options = {})
540539
end
541540
end
542541

542+
def magic_comment_matcher
543+
Regexp.new(/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/)
544+
end
545+
546+
def magic_comments_as_string(content)
547+
magic_comments = content.scan(magic_comment_matcher).flatten.compact
548+
549+
if magic_comments.any?
550+
magic_comments.join + "\n"
551+
else
552+
''
553+
end
554+
end
555+
543556
def remove_annotation_of_file(file_name, options = {})
544557
if File.exist?(file_name)
545558
content = File.read(file_name)

spec/annotate/annotate_models_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,30 @@ class User < ActiveRecord::Base
14761476
end
14771477
end
14781478

1479+
it 'adds an empty line between magic comments and annotation (position :before)' do
1480+
content = "class User < ActiveRecord::Base\nend\n"
1481+
magic_comments_list_each do |magic_comment|
1482+
model_file_name, = write_model 'user.rb', "#{magic_comment}\n#{content}"
1483+
1484+
annotate_one_file position: :before
1485+
schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info')
1486+
1487+
expect(File.read(model_file_name)).to eq("#{magic_comment}\n\n#{schema_info}\n#{content}")
1488+
end
1489+
end
1490+
1491+
it 'adds an empty line between magic comments and model file content (position :after)' do
1492+
content = "class User < ActiveRecord::Base\nend\n"
1493+
magic_comments_list_each do |magic_comment|
1494+
model_file_name, = write_model 'user.rb', "#{magic_comment}\n#{content}"
1495+
1496+
annotate_one_file position: :after
1497+
schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info')
1498+
1499+
expect(File.read(model_file_name)).to eq("#{magic_comment}\n\n#{content}\n#{schema_info}")
1500+
end
1501+
end
1502+
14791503
describe "if a file can't be annotated" do
14801504
before do
14811505
allow(AnnotateModels).to receive(:get_loaded_model).with('user').and_return(nil)

0 commit comments

Comments
 (0)