Sha256: a9984f68e00f48353b3b956509441bc1e32783ba9d6aa84db8a82019b6bc17ba

Contents?: true

Size: 1.09 KB

Versions: 8

Compression:

Stored size: 1.09 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 |d|
          if eager_loadable?
            preload(criteria.inclusions, d)
          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

8 entries across 7 versions & 2 rubygems

Version Path
mongoid-5.4.1 lib/mongoid/relations/eager.rb
mongoid-5.4.0 lib/mongoid/relations/eager.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/relations/eager.rb
tdiary-5.0.5 vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/relations/eager.rb
mongoid-5.2.1 lib/mongoid/relations/eager.rb
tdiary-5.0.4 vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/relations/eager.rb
mongoid-6.1.0 lib/mongoid/relations/eager.rb
mongoid-5.2.0 lib/mongoid/relations/eager.rb