Module: Mongoid::Relations::Embedded::Sort

Included in:
Builders::Embedded::Many
Defined in:
lib/mongoid/relations/embedded/sort.rb

Overview

This can be a performance killer on collections with many embedded documents

Instance Method Summary collapse

Instance Method Details

#sort_documents!(documents, metadata) ⇒ Object

Sorts documents

Parameters:



17
18
19
20
21
22
23
24
25
26
# File 'lib/mongoid/relations/embedded/sort.rb', line 17

def sort_documents!(documents, )
  sort_options = Criteria.new(.klass).order_by(.order).options[:sort]

  docs = documents.sort_by do |document|
    sort_options.map do |key, direction|
      Contexts::Enumerable::Sort.new(document.read_attribute(key), direction)
    end
  end
  documents.replace(docs)
end