Commit 7f317332 by Cuong Tran

Retain existing position unless :force is passed

parent 95401212
......@@ -232,27 +232,23 @@ module AnnotateModels
if old_columns == new_columns && !options[:force]
return false
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
# <<<<<<< HEAD
# # Replace the old schema info with the new schema info
# 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 new_content == old_content
# old_content.sub!(encoding, '')
# new_content = options[:position] == 'after' ?
# (encoding_header + (old_content =~ /\n$/ ? old_content : old_content + "\n") + info_block) :
# (encoding_header + info_block + old_content)
# end
# =======
# Strip the old schema info, and insert new schema info.
if new_content.end_with? (info_block + "\n")
new_content = old_content.sub(PATTERN, "\n" + info_block)
end
# if there *was* no old schema info (no substitution happened) or :force was passed,
# we simply need to insert it in correct position
if new_content == old_content || options[:force]
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)
end
File.open(file_name, "wb") { |f| f.puts new_content }
return true
......
......@@ -368,31 +368,67 @@ end
].each{|encoding_comment| yield encoding_comment }
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"
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
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
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
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
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
end
it "should update annotate position" do
describe "with existing annotation => :before" do
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
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 = another_schema_info
end
File.read(@model_file_name).should == "#{@file_content}\n#{another_schema_info}"
it "should retain current position" do
annotate_one_file
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
end
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
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