8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'app/models/concerns/positionable.rb', line 8
def move_to(position_arg)
position = [[position_arg, 0].max, self.class.count - 1].min
if self.position.nil? || position > (self.position)
DB.exec "
UPDATE #{self.class.table_name}
SET position = position - 1
WHERE position > :current_position and position <= :new_position",
current_position: self.position,
new_position: position
elsif position < self.position
DB.exec "
UPDATE #{self.class.table_name}
SET position = position + 1
WHERE position >= :new_position and position < :current_position",
current_position: self.position,
new_position: position
else
return
end
DB.exec "
UPDATE #{self.class.table_name}
SET position = :position
WHERE id = :id",
id: id,
position: position
self.position = position
end
|