Sha256: c450fb9e9fe160b53beb0a839aec4d27993ac74fb58096ac621f45d42993afd9

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

# WithAssignmentsBatch mirrors the WithAssignment mixin
# but implements operations in batches, so that they outperform
# their counterparts
module WithAssignmentsBatch
  extend ActiveSupport::Concern

  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, exercise: exercises).each do |it|
      pairs[it.exercise_id][0] = it
    end

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

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mumuki-domain-8.6.1 app/models/concerns/with_assignments_batch.rb
mumuki-domain-8.6.0 app/models/concerns/with_assignments_batch.rb
mumuki-domain-8.5.0 app/models/concerns/with_assignments_batch.rb
mumuki-domain-8.4.0 app/models/concerns/with_assignments_batch.rb