-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_sort_order.rb
50 lines (41 loc) · 1.62 KB
/
add_sort_order.rb
1
2
3
4
5
6
7
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
39
40
41
42
43
44
45
46
47
48
49
50
# add sort order to groups and questions that do not have it
# use the index position + 1 as the sort order
Dataset.each do |d|
puts "updating sort order for #{d.title}"
# first add sort order to main groups, then go through each subgroup
group_length = d.groups.main_groups.length
d.groups.main_groups.each_with_index do |g, i|
puts "- group #{g.title}"
g.sort_order = i+1 if g.sort_order == 0
# if group has subgroups add sort order to those too
g.arranged_items(include_groups: true).each_with_index do |subgroup, isg|
puts "-- subgroup #{subgroup.title}"
subgroup.sort_order = isg+1 if subgroup.sort_order == 0
end
end
# if there are groups, start the question index after the group indexes
d.questions.each_with_index do |q, i|
q.sort_order = i+1+group_length if q.sort_order.nil?
end
d.save
end
puts "----------------------------------"
TimeSeries.each do |d|
puts "updating sort order for #{d.title}"
# first add sort order to main groups, then go through each subgroup
group_length = d.groups.main_groups.length
d.groups.main_groups.each_with_index do |g, i|
puts "- group #{g.title}"
g.sort_order = i+1 if g.sort_order == 0
# if group has subgroups add sort order to those too
g.arranged_items(include_groups: true).each_with_index do |subgroup, isg|
puts "-- subgroup #{subgroup.title}"
subgroup.sort_order = isg+1 if subgroup.sort_order == 0
end
end
# if there are groups, start the question index after the group indexes
d.questions.each_with_index do |q, i|
q.sort_order = i+1+group_length if q.sort_order.nil?
end
d.save
end