Module: WithAssignmentsBatch

Extended by:
ActiveSupport::Concern
Included in:
Guide
Defined in:
app/models/concerns/with_assignments_batch.rb

Overview

WithAssignmentsBatch mirrors the WithAssignment mixin but implements operations in batches, so that they outperform their counterparts

Instance Method Summary collapse

Instance Method Details

#assignments_for(user, organization = Organization.current) ⇒ Object



26
27
28
29
30
# File 'app/models/concerns/with_assignments_batch.rb', line 26

def assignments_for(user, organization = Organization.current)
  find_assignments_for user, organization do |it, exercise|
    it || Assignment.build_for(user, exercise, organization)
  end
end

#find_assignments_for(user, organization = Organization.current, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/concerns/with_assignments_batch.rb', line 7

def find_assignments_for(user, organization = Organization.current, &block)
  block = block_given? ? block : lambda { |it, _e| it }

  return exercises.map { |it| block.call nil, it  } unless user

  pairs = exercises.map { |it| [it.id, [nil, it]] }.to_h
  Assignment.where(submitter: user, organization: organization, exercise: exercises).each do |it|
    pairs[it.exercise_id][0] = it
  end

  pairs.values.map { |assignment, exercise| block.call assignment, exercise }
end

#statuses_for(user, organization = Organization.current) ⇒ Object



20
21
22
23
24
# File 'app/models/concerns/with_assignments_batch.rb', line 20

def statuses_for(user, organization = Organization.current)
  find_assignments_for user, organization do |it|
    it&.status || Mumuki::Domain::Status::Submission::Pending
  end
end