correct ordering algorithm

This commit is contained in:
Sam 2013-10-21 16:14:09 +11:00
parent 29c8d2ebec
commit 31b73171dc
2 changed files with 9 additions and 1 deletions

View File

@ -9,6 +9,12 @@ module Concern
end end
def move_to(position) def move_to(position)
self.exec_sql "
UPDATE #{self.class.table_name}
SET position = position - 1
WHERE position > :position AND position > 0", {position: self.position}
self.exec_sql " self.exec_sql "
UPDATE #{self.class.table_name} UPDATE #{self.class.table_name}
SET position = :position SET position = :position

View File

@ -34,11 +34,13 @@ describe Concern::Positionable do
positions.should == [3,0,1,2,4] positions.should == [3,0,1,2,4]
TestItem.pluck(:position).sort.should == [0,1,2,3,4] TestItem.pluck(:position).sort.should == [0,1,2,3,4]
TestItem.find(3).move_to(1)
positions.should == [0,3,1,2,4]
# this is somewhat odd, but when there is not positioning # this is somewhat odd, but when there is not positioning
# not much we can do # not much we can do
TestItem.find(1).move_to(5) TestItem.find(1).move_to(5)
positions.should == [3,0,2,4,1] positions.should == [0,3,2,4,1]
TestItem.pluck(:position).sort.should == [0,1,2,3,4] TestItem.pluck(:position).sort.should == [0,1,2,3,4]