Sha256: ab198547fd3a2fad5b36c9d0930226bfe09d7bf5a476027369ea87b4d0938526

Contents?: true

Size: 1.1 KB

Versions: 7

Compression:

Stored size: 1.1 KB

Contents

# encoding: utf-8
require "mongoid/relations/eager/base"
require "mongoid/relations/eager/belongs_to"
require "mongoid/relations/eager/has_one"
require "mongoid/relations/eager/has_many"
require "mongoid/relations/eager/has_and_belongs_to_many"

module Mongoid
  module Relations
    module Eager

      def eager_loadable?
        !criteria.inclusions.empty?
      end

      def eager_load(docs)
        docs.tap do |docs|
          if eager_loadable?
            preload(criteria.inclusions, docs)
          end
        end
      end

      def preload(relations, docs)
        grouped_relations = relations.group_by do |metadata|
          metadata.inverse_class_name
        end
        grouped_relations.keys.each do |_klass|
          grouped_relations[_klass] = grouped_relations[_klass].group_by do |metadata|
            metadata.relation
          end
        end
        grouped_relations.each do |_klass, associations|
          docs = associations.collect do |_relation, association|
            _relation.eager_load_klass.new(association, docs).run
          end.flatten
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mongoid-5.2.0.rc0 lib/mongoid/relations/eager.rb
mongoid-6.1.0.rc0 lib/mongoid/relations/eager.rb
mongoid-6.0.3 lib/mongoid/relations/eager.rb
mongoid-5.1.6 lib/mongoid/relations/eager.rb
mongoid-6.0.2 lib/mongoid/relations/eager.rb
mongoid-6.0.1 lib/mongoid/relations/eager.rb
mongoid-5.1.5 lib/mongoid/relations/eager.rb