module Sorting

Public Class Methods

aggregate(collection, query, paginated_params, query_params) click to toggle source
# File lib/mumuki/classroom/models/sorting.rb, line 3
def self.aggregate(collection, query, paginated_params, query_params)
  reporting_pipeline = Reporting.build_pipeline(collection, query, paginated_params, query_params, projection)
  query = collection.collection.aggregate(pipeline(paginated_params, reporting_pipeline), allow_disk_use: true).first # Must allow disk use for sorting large collections by non-index
  query_results(query)
end
pipeline(params, pipeline) click to toggle source
# File lib/mumuki/classroom/models/sorting.rb, line 14
def self.pipeline(params, pipeline)
  paging_pipeline = []
  paging_pipeline << {'$skip': params[:page] * params[:per_page]}
  paging_pipeline << {'$limit': params[:per_page]}
  pipeline << {'$facet': {
    results: paging_pipeline,
    total: [
      {
        '$count': 'count'
      }
    ]
  }}
end
projection() click to toggle source
# File lib/mumuki/classroom/models/sorting.rb, line 28
def self.projection
  {
    '_id': 0,
    'assignments': 0,
    'notifications': 0,
    'guide._id': 0,
    'student._id': 0,
    'last_assignment._id': 0,
    'last_assignment.guide._id': 0,
    'last_assignment.exercise._id': 0,
    'last_assignment.submission._id': 0,
  }
end
query_results(query) click to toggle source
# File lib/mumuki/classroom/models/sorting.rb, line 9
def self.query_results(query)
  total = query[:total].first
  [total.blank? ? 0 : total[:count], query[:results]]
end