Skip to content

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

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
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
23 changes: 18 additions & 5 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,7 @@ def annotate_one_file(file_name, info_block, position, options = {})
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
Expand All @@ -522,13 +521,13 @@ def annotate_one_file(file_name, info_block, position, options = {})
# 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

Expand All @@ -540,6 +539,20 @@ def annotate_one_file(file_name, info_block, position, options = {})
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)
Expand Down
24 changes: 24 additions & 0 deletions spec/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,30 @@ class User < ActiveRecord::Base
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)
Expand Down