@@ -3,6 +3,8 @@ class Admin::GroupsController < ApplicationController
33 before_action :require_admin
44 before_action :find_event
55
6+ GROUP_SIZE = 4
7+
68 def index
79 @event_groups = @event . event_groups
810 @coaches_count = @event . coach_applications . approved . size
@@ -32,10 +34,14 @@ def regenerate
3234
3335 private
3436
37+ def fetch_by_key ( object , key )
38+ object [ key ] || [ ]
39+ end
40+
3541 def fill_groups
3642 # create a group for each pair of coaches
3743 @coaches = @event . coach_applications . approved . to_a
38- @coaches . each_slice ( 2 ) . with_index do |group , index |
44+ @coaches . each_slice ( 1 ) . with_index do |group , index |
3945 event_group = EventGroup . create ( event : @event , name : "Group #{ index + 1 } " )
4046 group . each do |coach_application |
4147 event_group . coach_applications << coach_application
@@ -46,27 +52,58 @@ def fill_groups
4652 @attendees = @event . applications . application_selected . confirmed . to_a
4753
4854 grouped_attendees_by_language = @attendees . group_by do |element |
49- [ element . language_de , element . language_en ]
55+ [ element . language_de , element . language_en , element . os ]
5056 end
5157
52- attendees_de = grouped_attendees_by_language [ [ true , false ] ]
53- attendees_en = grouped_attendees_by_language [ [ false , true ] ]
54- attendees_de_en = grouped_attendees_by_language [ [ true , true ] ]
55-
56- de_groups = attendees_de . in_groups_of ( 6 , false )
57- en_groups = attendees_en . in_groups_of ( 6 , false )
58+ attendees_de_mac = fetch_by_key ( grouped_attendees_by_language , [ true , false , "mac" ] )
59+ attendees_de_windows = fetch_by_key ( grouped_attendees_by_language , [ true , false , "windows" ] )
60+ attendees_de_linux = fetch_by_key ( grouped_attendees_by_language , [ true , false , "linux" ] )
61+
62+ attendees_en_mac = fetch_by_key ( grouped_attendees_by_language , [ false , true , "mac" ] )
63+ attendees_en_windows = fetch_by_key ( grouped_attendees_by_language , [ false , true , "windows" ] )
64+ attendees_en_linux = fetch_by_key ( grouped_attendees_by_language , [ false , true , "linux" ] )
65+
66+ attendees_de_en_mac = fetch_by_key ( grouped_attendees_by_language , [ true , true , "mac" ] )
67+ attendees_de_en_windows = fetch_by_key ( grouped_attendees_by_language , [ true , true , "windows" ] )
68+ attendees_de_en_linux = fetch_by_key ( grouped_attendees_by_language , [ true , true , "linux" ] )
69+
70+ attendees_de_mac_groups = attendees_de_mac . in_groups_of ( GROUP_SIZE , false )
71+ attendees_de_windows_groups = attendees_de_windows . in_groups_of ( GROUP_SIZE , false )
72+ attendees_de_linux_groups = attendees_de_linux . in_groups_of ( GROUP_SIZE , false )
73+ attendees_en_mac_groups = attendees_en_mac . in_groups_of ( GROUP_SIZE , false )
74+ attendees_en_windows_groups = attendees_en_windows . in_groups_of ( GROUP_SIZE , false )
75+ attendees_en_linux_groups = attendees_en_linux . in_groups_of ( GROUP_SIZE , false )
76+
77+ [ attendees_de_mac_groups , attendees_en_mac_groups ] . each do |groups |
78+ if groups . try :nonzero?
79+ if ( groups . last . size < GROUP_SIZE )
80+ groups . last . concat ( attendees_de_en_mac . pop ( GROUP_SIZE - groups . last . size ) )
81+ end
82+ end
83+ end
5884
59- if ( de_groups . last . size < 6 )
60- de_groups . last . concat ( attendees_de_en . pop ( 6 - de_groups . last . size ) )
85+ [ attendees_de_linux_groups , attendees_en_linux_groups ] . each do |groups |
86+ if groups . try :nonzero?
87+ if ( groups . last . size < GROUP_SIZE )
88+ groups . last . concat ( attendees_de_en_linux . pop ( GROUP_SIZE - groups . last . size ) )
89+ end
90+ end
6191 end
6292
63- if ( en_groups . last . size < 6 )
64- en_groups . last . concat ( attendees_de_en . pop ( 6 - en_groups . last . size ) )
93+ [ attendees_de_windows_groups , attendees_en_windows_groups ] . each do |groups |
94+ if groups . try :nonzero?
95+ if ( groups . last . size < GROUP_SIZE )
96+ groups . last . concat ( attendees_de_en_windows . pop ( GROUP_SIZE - groups . last . size ) )
97+ end
98+ end
6599 end
66100
67- de_en_groups = attendees_de_en . in_groups_of ( 6 , false )
101+ de_en_groups_mac = attendees_de_en_mac . in_groups_of ( GROUP_SIZE , false )
102+ de_en_groups_windows = attendees_de_en_windows . in_groups_of ( GROUP_SIZE , false )
103+ de_en_groups_linux = attendees_de_en_windows . in_groups_of ( GROUP_SIZE , false )
104+
105+ all_groups = attendees_de_mac_groups + attendees_de_windows_groups + attendees_de_linux_groups + attendees_en_mac_groups + attendees_en_windows_groups + attendees_en_linux_groups + de_en_groups_mac + de_en_groups_windows + de_en_groups_linux
68106
69- all_groups = de_groups + en_groups + de_en_groups
70107
71108 # FIXME: This can cause attendees to not be assigned to event groups
72109 @event . event_groups . each do |event_group |
0 commit comments