Module: EffectiveMentorshipsBulkGroup

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/effective_mentorships_bulk_group.rb

Overview

EffectiveCpdBulkAudit

Mark your owner model with effective_cpd_bulk_audit to get all the includes

Defined Under Namespace

Modules: Base, ClassMethods

Instance Method Summary collapse

Instance Method Details

#after_save_mentorship_group!(mentorship_group) ⇒ Object

Nothing to do. Override me.



365
366
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 365

def after_save_mentorship_group!(mentorship_group)
end

#build_mentorship_group(mentor_registration, any_mentee: false) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 203

def build_mentorship_group(mentor_registration, any_mentee: false)
  raise('expected a mentorship registration') unless mentor_registration.class.try(:effective_mentorships_registration?)
  raise('expected a mentor mentorship registration') unless mentor_registration.mentor?

  mentor = mentor_registration.user
  raise('expected a mentorship user') unless mentor.class.try(:effective_mentorships_user?)

  # Select the best matching mentees for this mentor registration
  mentee_registration = find_best_mentee_registration(mentor_registration)

  if any_mentee
    mentee_registration ||= find_any_mentee_registration(mentor_registration) 
  end

  return unless mentee_registration.present?

  mentee = mentee_registration.user
  raise('expected a mentorship user') unless mentee.class.try(:effective_mentorships_user?)

  # Create a new group in draft state
  mentorship_group = mentorship_groups.build(mentorship_cycle: mentorship_cycle, save_as_draft: true)
  mentorship_group.build_mentor(user: mentor)
  mentorship_group.build_mentee(user: mentee)

  # Return the group ready to be saved
  mentorship_group
end

#create_groups!Object

Called by Effective::MentorshipsBulkCreateGroupsJob



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 128

def create_groups!
  # First pass
  # Create groups with 1 mentor and 1 best matching mentee each
  EffectiveMentorships.MentorshipRegistration.uncached do
    # In-person
    mentors_mentorship_registrations.without_groups.in_person.find_each do |mentor_registration|
      mentorship_group = build_mentorship_group(mentor_registration)
      mentorship_group&.save!
    end

    # Either
    mentors_mentorship_registrations.without_groups.either.find_each do |mentor_registration|
      mentorship_group = build_mentorship_group(mentor_registration)
      mentorship_group&.save!
    end

    # Virtual
    mentors_mentorship_registrations.without_groups.virtual.find_each do |mentor_registration|
      mentorship_group = build_mentorship_group(mentor_registration)
      mentorship_group&.save!
    end

    # Second pass
    # Create groups with 1 mentor and any mentee
    mentors_mentorship_registrations.without_groups.find_each do |mentor_registration|
      mentorship_group = build_mentorship_group(mentor_registration, any_mentee: true)
      mentorship_group&.save!
    end

    # Third pass
    # Add best matching mentees to groups where mentor wants more than 1 mentee
    if max_pairings_mentee > 1
      fillable_mentors_mentorship_registrations = mentors_mentorship_registrations.multiple_mentees.with_groups_from(self)

      # In-person
      fillable_mentors_mentorship_registrations.in_person.find_each do |mentor_registration|
        mentorship_group = fill_mentorship_group(mentor_registration)
        mentorship_group&.save!
      end

      # Either
      fillable_mentors_mentorship_registrations.either.find_each do |mentor_registration|
        mentorship_group = fill_mentorship_group(mentor_registration)
        mentorship_group&.save!
      end

      # Virtual
      fillable_mentors_mentorship_registrations.virtual.find_each do |mentor_registration|
        mentorship_group = fill_mentorship_group(mentor_registration)
        mentorship_group&.save!
      end
    end

    # Fourth pass
    # Add any mentees to groups where mentor wants more than 1 mentee
    if max_pairings_mentee > 1
      fillable_mentors_mentorship_registrations = mentors_mentorship_registrations.multiple_mentees.with_groups_from(self)

      fillable_mentors_mentorship_registrations.find_each do |mentor_registration|
        mentorship_group = fill_mentorship_group(mentor_registration, any_mentee: true)
        mentorship_group&.save!
      end
    end

    # Call after_save callback on all mentorship groups
    mentorship_groups.each do |mentorship_group|
      mentorship_group.assign_attributes(save_as_draft: true)
      after_save_mentorship_group!(mentorship_group)
    end
  end

  wizard_steps[:grouping] ||= Time.zone.now
  save!
end

#fill_mentorship_group(mentor_registration, any_mentee: false) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 231

def fill_mentorship_group(mentor_registration, any_mentee: false)
  raise('expected a mentorship registration') unless mentor_registration.class.try(:effective_mentorships_registration?)
  raise('expected a mentor mentorship registration') unless mentor_registration.mentor?

  mentor = mentor_registration.user
  raise('expected a mentorship user') unless mentor.class.try(:effective_mentorships_user?)

  # Find the existing mentorship group that we previously created
  mentorship_group = mentorship_group_for(user: mentor)
  return unless mentorship_group.present?

  # Fill this group to limit of number of mentees
  limit = [mentor_registration.mentor_multiple_mentees_limit.to_i, max_pairings_mentee].min
  fill_mentees = (limit - mentorship_group.mentorship_group_mentees.length)
  return unless fill_mentees > 0

  # We only support 1 mentor and many mentees
  fill_mentees.times do
    mentee_registration = find_best_mentee_registration(mentor_registration)

    if any_mentee
      mentee_registration ||= find_any_mentee_registration(mentor_registration) 
    end

    if mentee_registration.present?
      mentee = mentee_registration.user
      raise('expected a mentorship user') unless mentee.class.try(:effective_mentorships_user?)

      mentorship_group.build_mentee(user: mentee)
    end
  end

  # Return the group ready to be saved
  mentorship_group.assign_attributes(save_as_draft: true)
  mentorship_group
end

#find_any_mentee_registration(mentor_registration) ⇒ Object



281
282
283
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 281

def find_any_mentee_registration(mentor_registration)
  mentees_mentorship_registrations.without_groups.order(:id).first
end

#find_best_either_mentee_registration(mentor_registration) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 304

def find_best_either_mentee_registration(mentor_registration)
  raise('expected an in-person mentor registration') unless mentor_registration.either?

  registrations = mentees_mentorship_registrations.without_groups.order(:id)

  # Either, same location, same category
  registration ||= registrations.either.where(location: mentor_registration.location, category: mentor_registration.category).first

  # In-person, same location, same category
  registration ||= registrations.in_person.where(location: mentor_registration.location, category: mentor_registration.category).first

  # Either, any location, same category
  registration ||= registrations.either.where(category: mentor_registration.category).first

  # In-person, same location, any category
  registration ||= registrations.in_person.where(location: mentor_registration.location).first

  # Virtual, same location, same category 
  registration ||= registrations.virtual.where(location: mentor_registration.location, category: mentor_registration.category).first

  # Virtual, any location, same category
  registration ||= registrations.virtual.where(category: mentor_registration.category).first

  # Either any
  registration ||= registrations.either.first

  # Virtual any
  registration ||= registrations.virtual.first

  # Might be nil
  registration
end

#find_best_in_person_mentee_registration(mentor_registration) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 285

def find_best_in_person_mentee_registration(mentor_registration)
  registrations = mentees_mentorship_registrations.without_groups.order(:id)

  # In-person, same location, same category
  registration ||= registrations.in_person.where(location: mentor_registration.location, category: mentor_registration.category).first

  # In-person, same location, any category
  registration ||= registrations.in_person.where(location: mentor_registration.location).first

  # Either, same location, same category
  registration ||= registrations.either.where(location: mentor_registration.location, category: mentor_registration.category).first

  # Either, same location, any category
  registration ||= registrations.either.where(location: mentor_registration.location).first

  # Might be nil
  registration
end

#find_best_mentee_registration(mentor_registration) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 268

def find_best_mentee_registration(mentor_registration)
  case mentor_registration.venue
  when 'In-person' 
    find_best_in_person_mentee_registration(mentor_registration)
  when 'Either' 
    find_best_either_mentee_registration(mentor_registration)
  when 'Virtual' 
    find_best_virtual_mentee_registration(mentor_registration)
  else
    raise("unexpected venue: #{mentor_registration.venue}")
  end
end

#find_best_virtual_mentee_registration(mentor_registration) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 337

def find_best_virtual_mentee_registration(mentor_registration)
  raise('expected an in-person mentor registration') unless mentor_registration.virtual?

  registrations = mentees_mentorship_registrations.without_groups.order(:id)

  # Virtual, same location, same category 
  registration ||= registrations.virtual.where(location: mentor_registration.location, category: mentor_registration.category).first

  # Virtual, any location, same category
  registration ||= registrations.virtual.where(category: mentor_registration.category).first

  # Either, same location, same category
  registration ||= registrations.either.where(location: mentor_registration.location, category: mentor_registration.category).first

  # Either, any location, same category
  registration ||= registrations.either.where(category: mentor_registration.category).first

  # Virtual any
  registration ||= registrations.virtual.first

  # Either any
  registration ||= registrations.either.first

  # Might be nil
  registration
end

#group!Object



89
90
91
92
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 89

def group!
  # Calls create_groups! and save!
  perform_with_job_status! { Effective::MentorshipsBulkCreateGroupsJob.perform_later(id) }
end

#max_pairings_menteeObject



70
71
72
73
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 70

def max_pairings_mentee
  raise('expected a mentorship cycle') unless mentorship_cycle.present?
  mentorship_cycle.max_pairings_mentee || 10 # Hardcode a sane limit. This shouldn't ever be used. The registrations is limited to 1-5
end

#mentees_mentorship_registrationsObject



80
81
82
83
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 80

def mentees_mentorship_registrations
  raise('expected a mentorship cycle') unless mentorship_cycle.present?
  EffectiveMentorships.MentorshipRegistration.opt_in.mentees.where(mentorship_cycle: mentorship_cycle)
end

#mentors_mentorship_registrationsObject



75
76
77
78
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 75

def mentors_mentorship_registrations
  raise('expected a mentorship cycle') unless mentorship_cycle.present?
  EffectiveMentorships.MentorshipRegistration.opt_in.mentors.where(mentorship_cycle: mentorship_cycle)
end

#mentorship_group_for(user:) ⇒ Object



85
86
87
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 85

def mentorship_group_for(user:)
  mentorship_groups.find { |mentorship_group| mentorship_group.mentorship_group_user(user: user).present? }
end

#notify!Object



99
100
101
102
103
104
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 99

def notify!
  return skip_notify! if email_form_skip?

  # Calls notify_groups! and save!
  perform_with_job_status! { Effective::MentorshipsBulkNotifyGroupsJob.perform_later(id) }
end

#notify_groups!Object

Called by Effective::MentorshipsBulkNotifyGroupsJob



118
119
120
121
122
123
124
125
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 118

def notify_groups!
  mentorship_groups.published.not_notified.find_each do |mentorship_group|
    mentorship_group.notify!
  end

  wizard_steps[:notifying] ||= Time.zone.now
  save!
end

#notifying!Object



112
113
114
115
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 112

def notifying!
  wizard_steps[last_wizard_step] ||= Time.zone.now # Also finish the whole wizard
  save!
end

#publish!Object



94
95
96
97
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 94

def publish!
  mentorship_groups.deep.find_each { |mentorship_group| mentorship_group.publish! }
  save!
end

#skip_notify!Object



106
107
108
109
110
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 106

def skip_notify!
  wizard_steps[:notifying] ||= Time.zone.now
  wizard_steps[last_wizard_step] ||= Time.zone.now
  save!
end

#to_sObject



66
67
68
# File 'app/models/concerns/effective_mentorships_bulk_group.rb', line 66

def to_s
  [model_name.human, ("##{id}" if id.present?)].compact.join(' ')
end