Skip to content

Commit a29aa47

Browse files
committed
Merge pull request #339 from kamilbielawski/persisting-wrapper-open-fix
Don't leave wrapper when removing annotations
2 parents 0906722 + c691120 commit a29aa47

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

lib/annotate/annotate_models.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,11 @@ def annotate_one_file(file_name, info_block, position, options={})
403403
end
404404
end
405405

406-
def remove_annotation_of_file(file_name)
406+
def remove_annotation_of_file(file_name, options={})
407407
if File.exist?(file_name)
408408
content = File.read(file_name)
409-
content.sub!(PATTERN, '')
409+
wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ""
410+
content.sub!(/(#{wrapper_open})?#{PATTERN}/, '')
410411

411412
File.open(file_name, 'wb') { |f| f.puts content }
412413

@@ -623,13 +624,13 @@ def remove_annotations(options={})
623624
model_name = klass.name.underscore
624625
table_name = klass.table_name
625626
model_file_name = file
626-
deannotated_klass = true if remove_annotation_of_file(model_file_name)
627+
deannotated_klass = true if remove_annotation_of_file(model_file_name, options)
627628

628629
get_patterns(matched_types(options)).
629630
map { |f| resolve_filename(f, model_name, table_name) }.
630631
each do |f|
631632
if File.exist?(f)
632-
remove_annotation_of_file(f)
633+
remove_annotation_of_file(f, options)
633634
deannotated_klass = true
634635
end
635636
end

spec/annotate/annotate_models_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,55 @@ class Foo < ActiveRecord::Base
511511

512512
expect(content(path)).to eq <<-EOS
513513
class Foo < ActiveRecord::Base
514+
end
515+
EOS
516+
end
517+
518+
it "should remove opening wrapper" do
519+
path = create "opening_wrapper.rb", <<-EOS
520+
# wrapper
521+
# == Schema Information
522+
#
523+
# Table name: foo
524+
#
525+
# id :integer not null, primary key
526+
# created_at :datetime
527+
# updated_at :datetime
528+
#
529+
530+
class Foo < ActiveRecord::Base
531+
end
532+
EOS
533+
534+
AnnotateModels.remove_annotation_of_file(path, wrapper_open: 'wrapper')
535+
536+
expect(content(path)).to eq <<-EOS
537+
class Foo < ActiveRecord::Base
538+
end
539+
EOS
540+
end
541+
542+
it "should remove closing wrapper" do
543+
path = create "closing_wrapper.rb", <<-EOS
544+
class Foo < ActiveRecord::Base
545+
end
546+
547+
# == Schema Information
548+
#
549+
# Table name: foo
550+
#
551+
# id :integer not null, primary key
552+
# created_at :datetime
553+
# updated_at :datetime
554+
#
555+
# wrapper
556+
557+
EOS
558+
559+
AnnotateModels.remove_annotation_of_file(path, wrapper_close: 'wrapper')
560+
561+
expect(content(path)).to eq <<-EOS
562+
class Foo < ActiveRecord::Base
514563
end
515564
EOS
516565
end

0 commit comments

Comments
 (0)