Sha256: 44543b2e2d6480af34d50bed413c7d2aca2cf861ab9fab51df5d607d6db7dda5

Contents?: true

Size: 1.75 KB

Versions: 38

Compression:

Stored size: 1.75 KB

Contents

module Bullet
  module Mongoid
    def self.enable
      require 'mongoid'
      ::Mongoid::Contextual::Mongo.class_eval do
        alias_method :origin_first, :first
        alias_method :origin_last, :last
        alias_method :origin_each, :each
        alias_method :origin_eager_load, :eager_load

        def first
          result = origin_first
          Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
          result
        end

        def last
          result = origin_last
          Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
          result
        end

        def each(&block)
          records = query.map{ |doc| ::Mongoid::Factory.from_db(klass, doc) }
          if records.length > 1
            Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
          elsif records.size == 1
            Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
          end
          origin_each(&block)
        end

        def eager_load(docs)
          associations = criteria.inclusions.map(&:name)
          docs.each do |doc|
            Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations)
          end
          Bullet::Detector::UnusedEagerLoading.add_eager_loadings(docs, associations)
          origin_eager_load(docs)
        end
      end

      ::Mongoid::Relations::Accessors.class_eval do
        alias_method :origin_get_relation, :get_relation

        def get_relation(name, metadata, object, reload = false)
          result = origin_get_relation(name, metadata, object, reload)
          if metadata.macro !~ /embed/
            Bullet::Detector::NPlusOneQuery.call_association(self, name)
          end
          result
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 2 rubygems

Version Path
bullet-4.14.6 lib/bullet/mongoid4x.rb
bullet-4.14.5 lib/bullet/mongoid4x.rb
bullet_instructure-4.0.5 lib/bullet/mongoid4x.rb
bullet-4.14.4 lib/bullet/mongoid4x.rb
bullet-4.14.3 lib/bullet/mongoid4x.rb
bullet-4.14.2 lib/bullet/mongoid4x.rb
bullet-4.14.0 lib/bullet/mongoid4x.rb
bullet-4.13.2 lib/bullet/mongoid4x.rb
bullet-4.13.1 lib/bullet/mongoid4x.rb
bullet_instructure-4.0.3 lib/bullet/mongoid4x.rb
bullet-4.13.0 lib/bullet/mongoid4x.rb
bullet-4.12.0 lib/bullet/mongoid4x.rb
bullet_instructure-4.0.2 lib/bullet/mongoid4x.rb
bullet-4.11.3 lib/bullet/mongoid4x.rb
bullet-4.11.2 lib/bullet/mongoid4x.rb
bullet-4.11.1 lib/bullet/mongoid4x.rb
bullet-4.11.0 lib/bullet/mongoid4x.rb
bullet-4.10.0 lib/bullet/mongoid4x.rb