Skip to content

Commit a816255

Browse files
xrav3nzctran
authored andcommitted
FIX: Ensure only one line is around the annotation (#669)
1 parent 693abdd commit a816255

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/annotate/annotate_models.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,9 @@ def annotate_one_file(file_name, info_block, position, options = {})
540540
new_content = if %w(after bottom).include?(options[position].to_s)
541541
magic_comments_block + (old_content.rstrip + "\n\n" + wrapped_info_block)
542542
elsif magic_comments_block.empty?
543-
magic_comments_block + wrapped_info_block + "\n" + old_content
543+
magic_comments_block + wrapped_info_block + "\n" + old_content.lstrip
544544
else
545-
magic_comments_block + "\n" + wrapped_info_block + "\n" + old_content
545+
magic_comments_block + "\n" + wrapped_info_block + "\n" + old_content.lstrip
546546
end
547547
else
548548
# replace the old annotation with the new one

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,18 @@ class User < ActiveRecord::Base
18031803
end
18041804
end
18051805

1806+
it 'only keeps a single empty line around the annotation (position :before)' do
1807+
content = "class User < ActiveRecord::Base\nend\n"
1808+
magic_comments_list_each do |magic_comment|
1809+
schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info')
1810+
model_file_name, = write_model 'user.rb', "#{magic_comment}\n\n\n\n#{content}"
1811+
1812+
annotate_one_file position: :before
1813+
1814+
expect(File.read(model_file_name)).to eq("#{magic_comment}\n\n#{schema_info}\n#{content}")
1815+
end
1816+
end
1817+
18061818
it 'does not change whitespace between magic comments and model file content (position :after)' do
18071819
content = "class User < ActiveRecord::Base\nend\n"
18081820
magic_comments_list_each do |magic_comment|

0 commit comments

Comments
 (0)