Skip to content

Commit f1fe52e

Browse files
sashabelozerovctran
authored andcommitted
[Fix #345] annotate --delete should not ignore SkipSchemaAnnotations flag
1 parent e315405 commit f1fe52e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/annotate/annotate_models.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module AnnotateModels
1212
PREFIX_MD = '## Schema Information'.freeze
1313
END_MARK = '== Schema Information End'.freeze
1414

15+
SKIP_ANNOTATION_PREFIX = '# -\*- SkipSchemaAnnotations'.freeze
16+
1517
MATCHED_TYPES = %w(test fixture factory serializer scaffold controller helper).freeze
1618

1719
# File.join for windows reverse bar compat?
@@ -498,7 +500,7 @@ def get_foreign_key_info(klass, options = {})
498500
def annotate_one_file(file_name, info_block, position, options = {})
499501
if File.exist?(file_name)
500502
old_content = File.read(file_name)
501-
return false if old_content =~ /# -\*- SkipSchemaAnnotations.*\n/
503+
return false if old_content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/
502504

503505
# Ignore the Schema version line because it changes with each migration
504506
header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/
@@ -562,6 +564,8 @@ def magic_comments_as_string(content)
562564
def remove_annotation_of_file(file_name, options = {})
563565
if File.exist?(file_name)
564566
content = File.read(file_name)
567+
return false if content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/
568+
565569
wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ''
566570
content.sub!(/(#{wrapper_open})?#{annotate_pattern(options)}/, '')
567571

@@ -767,7 +771,7 @@ def do_annotations(options = {})
767771

768772
def annotate_model_file(annotated, file, header, options)
769773
begin
770-
return false if /# -\*- SkipSchemaAnnotations.*/ =~ (File.exist?(file) ? File.read(file) : '')
774+
return false if /#{SKIP_ANNOTATION_PREFIX}.*/ =~ (File.exist?(file) ? File.read(file) : '')
771775
klass = get_model_class(file)
772776
do_annotate = klass &&
773777
klass < ActiveRecord::Base &&

spec/annotate/annotate_models_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,28 @@ class Foo < ActiveRecord::Base
14001400
end
14011401
EOS
14021402
end
1403+
1404+
it 'does not change file with #SkipSchemaAnnotations' do
1405+
content = <<-EOS
1406+
# -*- SkipSchemaAnnotations
1407+
# == Schema Information
1408+
#
1409+
# Table name: foo
1410+
#
1411+
# id :integer not null, primary key
1412+
# created_at :datetime
1413+
# updated_at :datetime
1414+
#
1415+
1416+
class Foo < ActiveRecord::Base
1417+
end
1418+
EOS
1419+
1420+
path = create 'skip.rb', content
1421+
1422+
AnnotateModels.remove_annotation_of_file(path)
1423+
expect(content(path)).to eq(content)
1424+
end
14031425
end
14041426

14051427
describe '#resolve_filename' do

0 commit comments

Comments
 (0)