Sha256: e63cb81fdcb869e742bc658c44bbaa9f3369792867780a030a868d9774c23e18

Contents?: true

Size: 1.67 KB

Versions: 40

Compression:

Stored size: 1.67 KB

Contents

module Bullet
  module Mongoid
    def self.enable
      require 'mongoid'

      ::Mongoid::Contexts::Mongo.class_eval do
        alias_method :origin_first, :first
        alias_method :origin_last, :last
        alias_method :origin_iterate, :iterate
        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 iterate(&block)
          records = execute.to_a
          if records.size > 1
            Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
          elsif records.size == 1
            Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
          end
          origin_iterate(&block)
        end

        def eager_load(docs)
          associations = criteria.inclusions.map(&:name)
          docs.each do |doc|
            Bullet::Detector::Association.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_set_relation, :set_relation

        def set_relation(name, relation)
          if relation && relation.metadata.macro !~ /embed/
            Bullet::Detector::NPlusOneQuery.call_association(self, name)
          end
          origin_set_relation(name, relation)
        end
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 2 rubygems

Version Path
bullet-5.4.3 lib/bullet/mongoid2x.rb
bullet-5.4.2 lib/bullet/mongoid2x.rb
bullet-5.4.1 lib/bullet/mongoid2x.rb
bullet-5.4.0 lib/bullet/mongoid2x.rb
bullet-5.3.0 lib/bullet/mongoid2x.rb
bullet-5.2.1 lib/bullet/mongoid2x.rb
bullet-5.2.0 lib/bullet/mongoid2x.rb
bullet-5.1.1 lib/bullet/mongoid2x.rb
bullet-5.1.0 lib/bullet/mongoid2x.rb
bullet-5.0.0 lib/bullet/mongoid2x.rb
bullet-4.14.10 lib/bullet/mongoid2x.rb
bullet-4.14.9 lib/bullet/mongoid2x.rb
bullet-4.14.8 lib/bullet/mongoid2x.rb
bullet_instructure-4.14.8 lib/bullet/mongoid2x.rb
bullet-4.14.7 lib/bullet/mongoid2x.rb
bullet_instructure-4.14.7 lib/bullet/mongoid2x.rb
bullet-4.14.6 lib/bullet/mongoid2x.rb
bullet-4.14.5 lib/bullet/mongoid2x.rb
bullet_instructure-4.0.5 lib/bullet/mongoid2x.rb
bullet-4.14.4 lib/bullet/mongoid2x.rb