module Bullet::Mongoid

def self.enable

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)
      return to_enum unless block
      records = []
      origin_each { |record| records << record }
      if records.length > 1
        Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
      elsif records.size == 1
        Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
      end
      records.each(&block)
    end
    def eager_load(docs)
      associations = criteria.inclusions.map(&:name)
      docs.each { |doc| Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations) }
      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)
      Bullet::Detector::NPlusOneQuery.call_association(self, name) if metadata.macro !~ /embed/
      result
    end
  end
end

def self.enable

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)
      return to_enum unless block
      records = []
      origin_each { |record| records << record }
      if records.length > 1
        Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
      elsif records.size == 1
        Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
      end
      records.each(&block)
    end
    def eager_load(docs)
      associations = criteria.inclusions.map(&:name)
      docs.each { |doc| Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations) }
      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)
      Bullet::Detector::NPlusOneQuery.call_association(self, name) if metadata.macro !~ /embed/
      result
    end
  end
end

def self.enable

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(opt = {})
      result = origin_first(opt)
      Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
      result
    end
    def last(opt = {})
      result = origin_last(opt)
      Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
      result
    end
    def each(&block)
      return to_enum unless block
      records = []
      origin_each { |record| records << record }
      if records.length > 1
        Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
      elsif records.size == 1
        Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
      end
      records.each(&block)
    end
    def eager_load(docs)
      associations = criteria.inclusions.map(&:name)
      docs.each { |doc| Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations) }
      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)
      Bullet::Detector::NPlusOneQuery.call_association(self, name) if metadata.macro !~ /embed/
      result
    end
  end
end

def self.enable

def self.enable
  require 'mongoid'
  require 'rubygems'
  ::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
    %i[first last].each do |context|
      default = Gem::Version.new(::Mongoid::VERSION) >= Gem::Version.new('7.5') ? nil : {}
      define_method(context) do |opts = default|
        result = send(:"origin_#{context}", opts)
        Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
        result
      end
    end
    def each(&block)
      return to_enum unless block_given?
      first_document = nil
      document_count = 0
      origin_each do |document|
        document_count += 1
        if document_count == 1
          first_document = document
        elsif document_count == 2
          Bullet::Detector::NPlusOneQuery.add_possible_objects([first_document, document])
          yield(first_document)
          first_document = nil
          yield(document)
        else
          Bullet::Detector::NPlusOneQuery.add_possible_objects(document)
          yield(document)
        end
      end
      if document_count == 1
        Bullet::Detector::NPlusOneQuery.add_impossible_object(first_document)
        yield(first_document)
      end
      self
    end
    def eager_load(docs)
      associations = criteria.inclusions.map(&:name)
      docs.each { |doc| Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations) }
      Bullet::Detector::UnusedEagerLoading.add_eager_loadings(docs, associations)
      origin_eager_load(docs)
    end
  end
  ::Mongoid::Association::Accessors.class_eval do
    alias_method :origin_get_relation, :get_relation
    def get_relation(name, association, object, reload = false)
      result = origin_get_relation(name, association, object, reload)
      Bullet::Detector::NPlusOneQuery.call_association(self, name) unless association.embedded?
      result
    end
  end
end

def each(&block)

def each(&block)
  return to_enum unless block
  records = []
  origin_each { |record| records << record }
  if records.length > 1
    Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
  elsif records.size == 1
    Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
  end
  records.each(&block)
end

def each(&block)

def each(&block)
  return to_enum unless block
  records = []
  origin_each { |record| records << record }
  if records.length > 1
    Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
  elsif records.size == 1
    Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
  end
  records.each(&block)
end

def each(&block)

def each(&block)
  return to_enum unless block
  records = []
  origin_each { |record| records << record }
  if records.length > 1
    Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
  elsif records.size == 1
    Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
  end
  records.each(&block)
end

def each(&block)

def each(&block)
  return to_enum unless block_given?
  first_document = nil
  document_count = 0
  origin_each do |document|
    document_count += 1
    if document_count == 1
      first_document = document
    elsif document_count == 2
      Bullet::Detector::NPlusOneQuery.add_possible_objects([first_document, document])
      yield(first_document)
      first_document = nil
      yield(document)
    else
      Bullet::Detector::NPlusOneQuery.add_possible_objects(document)
      yield(document)
    end
  end
  if document_count == 1
    Bullet::Detector::NPlusOneQuery.add_impossible_object(first_document)
    yield(first_document)
  end
  self
end

def eager_load(docs)

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

def eager_load(docs)

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

def eager_load(docs)

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

def eager_load(docs)

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

def first

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

def first

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

def first(opt = {})

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

def get_relation(name, metadata, object, reload = false)

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

def get_relation(name, metadata, object, reload = false)

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

def get_relation(name, metadata, object, reload = false)

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

def get_relation(name, association, object, reload = false)

def get_relation(name, association, object, reload = false)
  result = origin_get_relation(name, association, object, reload)
  Bullet::Detector::NPlusOneQuery.call_association(self, name) unless association.embedded?
  result
end

def last

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

def last

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

def last(opt = {})

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