lib/mongoid/contextual/mongo.rb in mongoid-3.0.15 vs lib/mongoid/contextual/mongo.rb in mongoid-3.0.16

- old
+ new

@@ -1,28 +1,27 @@ # encoding: utf-8 require "mongoid/contextual/atomic" require "mongoid/contextual/aggregable/mongo" require "mongoid/contextual/command" +require "mongoid/contextual/eager" require "mongoid/contextual/find_and_modify" require "mongoid/contextual/map_reduce" module Mongoid module Contextual class Mongo include Enumerable include Aggregable::Mongo include Atomic + include Eager # @attribute [r] collection The collection to query against. # @attribute [r] criteria The criteria for the context. # @attribute [r] klass The klass for the criteria. # @attribute [r] query The Moped query. attr_reader :collection, :criteria, :klass, :query - # @attribute [rw] eager_loaded Has the context been eager loaded? - attr_accessor :eager_loaded - # Is the enumerable of matching documents empty? # # @example Is the context empty? # context.blank? # @@ -64,11 +63,11 @@ # @return [ Integer ] The number of matches. # # @since 3.0.0 def count(document = nil, &block) return super(&block) if block_given? - return query.count unless document + return (cached? ? @count ||= query.count : query.count) unless document collection.find(criteria.and(_id: document.id).selector).count end # Delete all documents in the database that match the selector. # @@ -77,11 +76,11 @@ # # @return [ nil ] Nil. # # @since 3.0.0 def delete - query.count.tap do + self.count.tap do query.remove_all end end alias :delete_all :delete @@ -92,11 +91,11 @@ # # @return [ nil ] Nil. # # @since 3.0.0 def destroy - destroyed = query.count + destroyed = self.count each do |doc| doc.destroy end destroyed end @@ -240,11 +239,11 @@ # # @return [ Integer ] The number of documents. # # @since 3.0.0 def length - @length ||= query.count + @length ||= self.count end alias :size :length # Limits the number of documents that are returned from the database. # @@ -521,60 +520,10 @@ else query end end - # Eager load the inclusions for the provided documents. - # - # @example Eager load the inclusions. - # context.eager_load(docs) - # - # @param [ Array<Document> ] docs The docs returning from the db. - # - # @return [ true ] Always true. - # - # @since 3.0.0 - def eager_load(docs) - criteria.inclusions.reject! do |metadata| - metadata.eager_load(eager_loaded_ids(docs, metadata)) if !docs.empty? - end - self.eager_loaded = true - end - - # Get the ids that to be used to eager load documents. - # - # @api private - # - # @example Get the ids. - # context.eager_load(docs, metadata) - # - # @param [ Array<Document> ] docs The pre-loaded documents. - # @param [ Metadata ] metadata The relation metadata. - # - # @return [ Array<Object> ] The ids. - # - # @since 3.0.0 - def eager_loaded_ids(docs, metadata) - if metadata.stores_foreign_key? - docs.flat_map{ |doc| doc.send(metadata.foreign_key) } - else - docs.map(&:id) - end - end - - # Is this context able to be eager loaded? - # - # @example Is the context eager loadable? - # context.eager_loadable? - # - # @return [ true, false ] If the context is able to be eager loaded. - # - # @since 3.0.0 - def eager_loadable? - !eager_loaded && !criteria.inclusions.empty? - end - # Apply all the optional criterion. # # @example Apply the options. # context.apply_options # @@ -603,29 +552,9 @@ fields = criteria.options[:fields] Threaded.set_selection(criteria.object_id, fields) unless fields.blank? yield ensure Threaded.delete_selection(criteria.object_id) - end - end - - # If the provided document exists, eager load it's dependencies or return - # nil. - # - # @example Eager load if the document is not nil. - # context.with_eager_loading(document) - # - # @param [ Hash ] document The document from the database. - # - # @return [ Document, nil ] The instantiated model document. - # - # @since 3.0.0 - def with_eager_loading(document) - selecting do - return nil unless document - doc = Factory.from_db(klass, document, criteria.object_id) - eager_load([ doc ]) if eager_loadable? - doc end end # Yield to the document. #