Module: Sorting

Defined in:
lib/mumuki/classroom/models/sorting.rb,
lib/mumuki/classroom/models/sorting/student.rb,
lib/mumuki/classroom/models/sorting/guide_progress.rb

Defined Under Namespace

Modules: GuideProgress, Student Classes: SortBy

Class Method Summary collapse

Class Method Details

.aggregate(collection, query, paginated_params, query_params) ⇒ Object



3
4
5
6
7
# 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) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

.projectionObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



9
10
11
12
# 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