Sha256: c998e8773e33039de0c51a856133b512e226752477736bd94bf132ddab7917b2

Contents?: true

Size: 1.11 KB

Versions: 16

Compression:

Stored size: 1.11 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

      attr_accessor :eager_loaded

      def with_eager_loading(document)
        return nil unless document
        doc = Factory.from_db(klass, document, criteria.options[:fields])
        eager_load_one(doc)
        doc
      end

      def eager_load_one(doc)
        eager_load([doc])
      end

      def eager_loadable?(document = nil)
        return false if criteria.inclusions.empty?
        !eager_loaded
      end

      def eager_load(docs)
        return false unless eager_loadable?
        preload(criteria.inclusions, docs)
        self.eager_loaded = true
      end

      def preload(relations, docs)

        relations.group_by do |metadata|
          metadata.relation
        end.each do |relation, associations|
          relation.eager_load_klass.new(associations, docs).run
        end
      end
    end
  end
end

Version data entries

16 entries across 14 versions & 3 rubygems

Version Path
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongoid-4.0.2/lib/mongoid/relations/eager.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/mongoid-5.0.2/lib/mongoid/relations/eager.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.2/lib/mongoid/relations/eager.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.1/lib/mongoid/relations/eager.rb
mongoid-5.0.2 lib/mongoid/relations/eager.rb
mongoid-5.0.1 lib/mongoid/relations/eager.rb
mongoid-5.0.0 lib/mongoid/relations/eager.rb
mongoid-5.0.0.rc0 lib/mongoid/relations/eager.rb
mongoid-5.0.0.beta lib/mongoid/relations/eager.rb
mongoid-4.0.2 lib/mongoid/relations/eager.rb
mongoid-4.0.1 lib/mongoid/relations/eager.rb
mongoid-4.0.0 lib/mongoid/relations/eager.rb
mongoid-4.0.0.rc2 lib/mongoid/relations/eager.rb
mongoid-4.0.0.rc1 lib/mongoid/relations/eager.rb
mongoid-4.0.0.beta2 lib/mongoid/relations/eager.rb
mongoid-4.0.0.beta1 lib/mongoid/relations/eager.rb