correct ordering algorithm
This commit is contained in:
parent
29c8d2ebec
commit
31b73171dc
|
@ -9,6 +9,12 @@ module Concern
|
|||
end
|
||||
|
||||
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 "
|
||||
UPDATE #{self.class.table_name}
|
||||
SET position = :position
|
||||
|
|
|
@ -34,11 +34,13 @@ describe Concern::Positionable do
|
|||
positions.should == [3,0,1,2,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
|
||||
# not much we can do
|
||||
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]
|
||||
|
||||
|
|
Loading…
Reference in New Issue