Commit 7f317332 by Cuong Tran

Retain existing position unless :force is passed

parent 95401212
...@@ -232,27 +232,23 @@ module AnnotateModels ...@@ -232,27 +232,23 @@ module AnnotateModels
if old_columns == new_columns && !options[:force] if old_columns == new_columns && !options[:force]
return false return false
else else
# Replace inline the old schema info with the new schema info
new_content = old_content.sub(PATTERN, info_block + "\n")
# todo: figure out if we need to extract any logic from this merge chunk if new_content.end_with? (info_block + "\n")
# <<<<<<< HEAD new_content = old_content.sub(PATTERN, "\n" + info_block)
# # Replace the old schema info with the new schema info end
# new_content = old_content.sub(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n*/, info_block)
# # But, if there *was* no old schema info, we simply need to insert it # if there *was* no old schema info (no substitution happened) or :force was passed,
# if new_content == old_content # we simply need to insert it in correct position
# old_content.sub!(encoding, '') if new_content == old_content || options[:force]
# new_content = options[:position] == 'after' ? old_content.sub!(encoding, '')
# (encoding_header + (old_content =~ /\n$/ ? old_content : old_content + "\n") + info_block) : old_content.sub!(PATTERN, '')
# (encoding_header + info_block + old_content)
# end new_content = options[position].to_s == 'after' ?
# ======= (encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
(encoding_header + info_block + "\n" + old_content)
# Strip the old schema info, and insert new schema info. end
old_content.sub!(encoding, '')
old_content.sub!(PATTERN, '')
new_content = options[position].to_s == 'after' ?
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
(encoding_header + info_block + "\n" + old_content)
File.open(file_name, "wb") { |f| f.puts new_content } File.open(file_name, "wb") { |f| f.puts new_content }
return true return true
......
...@@ -368,31 +368,67 @@ end ...@@ -368,31 +368,67 @@ end
].each{|encoding_comment| yield encoding_comment } ].each{|encoding_comment| yield encoding_comment }
end end
it "should annotate the file before the model if position == 'before'" do it "should put annotation before class if :position == 'before'" do
annotate_one_file :position => "before" annotate_one_file :position => "before"
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}" File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
end end
it "should annotate before if given :position => :before" do it "should put annotation before class if :position => :before" do
annotate_one_file :position => :before annotate_one_file :position => :before
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}" File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
end end
it "should annotate after if given :position => :after" do it "should put annotation after class if :position => :after" do
annotate_one_file :position => :after annotate_one_file :position => :after
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}" File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
end end
it "should update annotate position" do describe "with existing annotation => :before" do
annotate_one_file :position => :before before do
annotate_one_file :position => :before
another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer),]),
"== Schema Info")
@schema_info = another_schema_info
end
it "should retain current position" do
annotate_one_file
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
end
it "should retain current position even when :position is changed to :after" do
annotate_one_file :position => :after
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
end
it "should change position to :after when :force => true" do
annotate_one_file :position => :after, :force => true
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
end
end
another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer),]), describe "with existing annotation => :after" do
before do
annotate_one_file :position => :after
another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer),]),
"== Schema Info") "== Schema Info")
@schema_info = another_schema_info
end
@schema_info = another_schema_info it "should retain current position" do
annotate_one_file :position => :after annotate_one_file
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
end
File.read(@model_file_name).should == "#{@file_content}\n#{another_schema_info}" it "should retain current position even when :position is changed to :before" do
annotate_one_file :position => :before
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
end
it "should change position to :before when :force => true" do
annotate_one_file :position => :before, :force => true
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
end
end end
it 'should skip columns with option[:ignore_columns] set' do it 'should skip columns with option[:ignore_columns] set' 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