Commit 992ad11a by Alexander Belozerov Committed by Cuong Tran

[Fix #464] Add an empty line between magic comment and schema (#491)

parent 1e2047d2
......@@ -503,8 +503,7 @@ module AnnotateModels
old_columns = old_header && old_header.scan(column_pattern).sort
new_columns = new_header && new_header.scan(column_pattern).sort
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)/)
magic_comments = old_content.scan(magic_comment_matcher).flatten.compact
magic_comments_block = magic_comments_as_string(old_content)
if old_columns == new_columns && !options[:force]
return false
......@@ -522,13 +521,13 @@ module AnnotateModels
# if there *was* no old schema info (no substitution happened) or :force was passed,
# we simply need to insert it in correct position
if new_content == old_content || options[:force]
old_content.sub!(magic_comment_matcher, '')
old_content.gsub!(magic_comment_matcher, '')
old_content.sub!(annotate_pattern(options), '')
new_content = if %w(after bottom).include?(options[position].to_s)
magic_comments.join + (old_content.rstrip + "\n\n" + wrapped_info_block)
magic_comments_block + (old_content.rstrip + "\n\n" + wrapped_info_block)
else
magic_comments.join + wrapped_info_block + "\n" + old_content
magic_comments_block + wrapped_info_block + "\n" + old_content
end
end
......@@ -540,6 +539,20 @@ module AnnotateModels
end
end
def magic_comment_matcher
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))/)
end
def magic_comments_as_string(content)
magic_comments = content.scan(magic_comment_matcher).flatten.compact
if magic_comments.any?
magic_comments.join + "\n"
else
''
end
end
def remove_annotation_of_file(file_name, options = {})
if File.exist?(file_name)
content = File.read(file_name)
......
......@@ -1476,6 +1476,30 @@ end
end
end
it 'adds an empty line between magic comments and annotation (position :before)' do
content = "class User < ActiveRecord::Base\nend\n"
magic_comments_list_each do |magic_comment|
model_file_name, = write_model 'user.rb', "#{magic_comment}\n#{content}"
annotate_one_file position: :before
schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info')
expect(File.read(model_file_name)).to eq("#{magic_comment}\n\n#{schema_info}\n#{content}")
end
end
it 'adds an empty line between magic comments and model file content (position :after)' do
content = "class User < ActiveRecord::Base\nend\n"
magic_comments_list_each do |magic_comment|
model_file_name, = write_model 'user.rb', "#{magic_comment}\n#{content}"
annotate_one_file position: :after
schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info')
expect(File.read(model_file_name)).to eq("#{magic_comment}\n\n#{content}\n#{schema_info}")
end
end
describe "if a file can't be annotated" do
before do
allow(AnnotateModels).to receive(:get_loaded_model).with('user').and_return(nil)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment