Commit a29aa470 by Cuong Tran

Merge pull request #339 from kamilbielawski/persisting-wrapper-open-fix

Don't leave wrapper when removing annotations
parents 09067223 c6911204
...@@ -403,10 +403,11 @@ module AnnotateModels ...@@ -403,10 +403,11 @@ module AnnotateModels
end end
end end
def remove_annotation_of_file(file_name) def remove_annotation_of_file(file_name, options={})
if File.exist?(file_name) if File.exist?(file_name)
content = File.read(file_name) content = File.read(file_name)
content.sub!(PATTERN, '') wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ""
content.sub!(/(#{wrapper_open})?#{PATTERN}/, '')
File.open(file_name, 'wb') { |f| f.puts content } File.open(file_name, 'wb') { |f| f.puts content }
...@@ -623,13 +624,13 @@ module AnnotateModels ...@@ -623,13 +624,13 @@ module AnnotateModels
model_name = klass.name.underscore model_name = klass.name.underscore
table_name = klass.table_name table_name = klass.table_name
model_file_name = file model_file_name = file
deannotated_klass = true if remove_annotation_of_file(model_file_name) deannotated_klass = true if remove_annotation_of_file(model_file_name, options)
get_patterns(matched_types(options)). get_patterns(matched_types(options)).
map { |f| resolve_filename(f, model_name, table_name) }. map { |f| resolve_filename(f, model_name, table_name) }.
each do |f| each do |f|
if File.exist?(f) if File.exist?(f)
remove_annotation_of_file(f) remove_annotation_of_file(f, options)
deannotated_klass = true deannotated_klass = true
end end
end end
......
...@@ -514,6 +514,55 @@ class Foo < ActiveRecord::Base ...@@ -514,6 +514,55 @@ class Foo < ActiveRecord::Base
end end
EOS EOS
end end
it "should remove opening wrapper" do
path = create "opening_wrapper.rb", <<-EOS
# wrapper
# == Schema Information
#
# Table name: foo
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
#
class Foo < ActiveRecord::Base
end
EOS
AnnotateModels.remove_annotation_of_file(path, wrapper_open: 'wrapper')
expect(content(path)).to eq <<-EOS
class Foo < ActiveRecord::Base
end
EOS
end
it "should remove closing wrapper" do
path = create "closing_wrapper.rb", <<-EOS
class Foo < ActiveRecord::Base
end
# == Schema Information
#
# Table name: foo
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
#
# wrapper
EOS
AnnotateModels.remove_annotation_of_file(path, wrapper_close: 'wrapper')
expect(content(path)).to eq <<-EOS
class Foo < ActiveRecord::Base
end
EOS
end
end end
describe '#resolve_filename' do describe '#resolve_filename' do
......
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