Skip to content

Commit 7f31733

Browse files
author
Cuong Tran
committed
Retain existing position unless :force is passed
1 parent 9540121 commit 7f31733

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-29
lines changed

lib/annotate/annotate_models.rb

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -232,27 +232,23 @@ def annotate_one_file(file_name, info_block, position, options={})
232232
if old_columns == new_columns && !options[:force]
233233
return false
234234
else
235+
# Replace inline the old schema info with the new schema info
236+
new_content = old_content.sub(PATTERN, info_block + "\n")
235237

236-
# todo: figure out if we need to extract any logic from this merge chunk
237-
# <<<<<<< HEAD
238-
# # Replace the old schema info with the new schema info
239-
# new_content = old_content.sub(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n*/, info_block)
240-
# # But, if there *was* no old schema info, we simply need to insert it
241-
# if new_content == old_content
242-
# old_content.sub!(encoding, '')
243-
# new_content = options[:position] == 'after' ?
244-
# (encoding_header + (old_content =~ /\n$/ ? old_content : old_content + "\n") + info_block) :
245-
# (encoding_header + info_block + old_content)
246-
# end
247-
# =======
248-
249-
# Strip the old schema info, and insert new schema info.
250-
old_content.sub!(encoding, '')
251-
old_content.sub!(PATTERN, '')
252-
253-
new_content = options[position].to_s == 'after' ?
254-
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
255-
(encoding_header + info_block + "\n" + old_content)
238+
if new_content.end_with? (info_block + "\n")
239+
new_content = old_content.sub(PATTERN, "\n" + info_block)
240+
end
241+
242+
# if there *was* no old schema info (no substitution happened) or :force was passed,
243+
# we simply need to insert it in correct position
244+
if new_content == old_content || options[:force]
245+
old_content.sub!(encoding, '')
246+
old_content.sub!(PATTERN, '')
247+
248+
new_content = options[position].to_s == 'after' ?
249+
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
250+
(encoding_header + info_block + "\n" + old_content)
251+
end
256252

257253
File.open(file_name, "wb") { |f| f.puts new_content }
258254
return true

spec/annotate/annotate_models_spec.rb

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -368,31 +368,67 @@ def encoding_comments_list_each
368368
].each{|encoding_comment| yield encoding_comment }
369369
end
370370

371-
it "should annotate the file before the model if position == 'before'" do
371+
it "should put annotation before class if :position == 'before'" do
372372
annotate_one_file :position => "before"
373373
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
374374
end
375375

376-
it "should annotate before if given :position => :before" do
376+
it "should put annotation before class if :position => :before" do
377377
annotate_one_file :position => :before
378378
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
379379
end
380380

381-
it "should annotate after if given :position => :after" do
381+
it "should put annotation after class if :position => :after" do
382382
annotate_one_file :position => :after
383383
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
384384
end
385385

386-
it "should update annotate position" do
387-
annotate_one_file :position => :before
386+
describe "with existing annotation => :before" do
387+
before do
388+
annotate_one_file :position => :before
389+
another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer),]),
390+
"== Schema Info")
391+
@schema_info = another_schema_info
392+
end
393+
394+
it "should retain current position" do
395+
annotate_one_file
396+
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
397+
end
398+
399+
it "should retain current position even when :position is changed to :after" do
400+
annotate_one_file :position => :after
401+
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
402+
end
403+
404+
it "should change position to :after when :force => true" do
405+
annotate_one_file :position => :after, :force => true
406+
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
407+
end
408+
end
388409

389-
another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer),]),
410+
describe "with existing annotation => :after" do
411+
before do
412+
annotate_one_file :position => :after
413+
another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer),]),
390414
"== Schema Info")
415+
@schema_info = another_schema_info
416+
end
391417

392-
@schema_info = another_schema_info
393-
annotate_one_file :position => :after
418+
it "should retain current position" do
419+
annotate_one_file
420+
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
421+
end
394422

395-
File.read(@model_file_name).should == "#{@file_content}\n#{another_schema_info}"
423+
it "should retain current position even when :position is changed to :before" do
424+
annotate_one_file :position => :before
425+
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
426+
end
427+
428+
it "should change position to :before when :force => true" do
429+
annotate_one_file :position => :before, :force => true
430+
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
431+
end
396432
end
397433

398434
it 'should skip columns with option[:ignore_columns] set' do

0 commit comments

Comments
 (0)