Sha256: d78d81eb0405286d7aaee65f8812b83e6952c202f3a257c2c180847f28489e13

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

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

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

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

        def iterate(&block)
          records = execute.to_a
          if records.size > 1
            Bullet::Detector::Association.add_possible_objects(records)
          elsif records.size == 1
            Bullet::Detector::Association.add_impossible_object(records.first)
          end
          origin_iterate(&block)
        end
      end

      ::Mongoid::Relations::Accessors.class_eval do
        alias_method :origin_set_relation, :set_relation

        def set_relation(name, relation)
          Bullet::Detector::NPlusOneQuery.call_association(self, name)
          origin_set_relation(name, relation)
        end
      end

      ::Mongoid::Contexts::Mongo.class_eval do
        alias_method :origin_eager_load, :eager_load

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bullet-4.0.0 lib/bullet/mongoid.rb