Sha256: 4b03ee1c15d42d23ae846cda566b6cac6525ff7549cbf248239c43f2eff2789b

Contents?: true

Size: 1.99 KB

Versions: 14

Compression:

Stored size: 1.99 KB

Contents

::Mongoid::Contextual::Mongo
module Mongoid
  module Contextual
    class Mongo
      def initialize(criteria)
        @criteria, @klass, @cache = criteria, criteria.klass, criteria.options[:cache]

        # Only line changed is here, get collection name from criteria, not @klass
        #@collection = @klass.with(criteria.persistence_options || {}).collection
        @collection = criteria.collection

        criteria.send(:merge_type_selection)
        @view = collection.find(criteria.selector)
        apply_options
      end

      #
      # Patches below add `criteria` as the last argument to `Factory.from_db`
      #
      def first
        return documents.first if cached? && cache_loaded?
        try_cache(:first) do
          if raw_doc = view.limit(-1).first
            doc = Factory.from_db(klass, raw_doc, criteria.options[:fields], criteria)
            eager_load([doc]).first
          end
        end
      end

      def find_first
        return documents.first if cached? && cache_loaded?
        if raw_doc = view.first
          doc = Factory.from_db(klass, raw_doc, criteria.options[:fields], criteria)
          eager_load([doc]).first
        end
      end

      def last
        try_cache(:last) do
          with_inverse_sorting do
            if raw_doc = view.limit(-1).first
              doc = Factory.from_db(klass, raw_doc, criteria.options[:fields], criteria)
              eager_load([doc]).first
            end
          end
        end
      end

      def documents_for_iteration
        return documents if cached? && !documents.empty?
        return view unless eager_loadable?
        docs = view.map{ |doc| Factory.from_db(klass, doc, criteria.options[:fields], criteria) }
        eager_load(docs)
      end

      def yield_document(document, &block)
        doc = document.respond_to?(:_id) ?
                document : Factory.from_db(klass, document, criteria.options[:fields], criteria)
        yield(doc)
        documents.push(doc) if cacheable?
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rocketjob-5.1.1 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-5.1.0 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-5.0.0 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-5.0.0.rc1 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-5.0.0.beta4 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-5.0.0.beta3 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-5.0.0.beta2 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-5.0.0.beta lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-4.3.0.beta2 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-4.3.0.beta lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-4.2.0 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-4.1.1 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-4.1.0 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb
rocketjob-4.0.0 lib/rocket_job/extensions/mongoid_5/contextual/mongo.rb