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
|
# File 'lib/mongoid/reorder.rb', line 10
def reorder_objects(ids)
objects = self.unscoped.find(ids)
max_weight = (objects.max { |a,b| a._position <=> b._position })._position
min_weight = (objects.min { |a,b| a._position <=> b._position })._position
if max_weight.nil? or max_weight == 0
max_weight = 100
end
if min_weight.nil?
min_weight = 0
end
weight_delta = (max_weight - min_weight) / objects.size
objects_dict = {}
objects.each { |o| objects_dict[o.id.to_s] = o }
weights = []
ids.each_with_index do |id, index|
position = max_weight - index * weight_delta
objects_dict[id].update_attributes!(_position: position)
end
"ok"
end
|