# frozen_string_literal: truemoduleBulletmoduleSaveWithBulletSupportdef_create_record(*)superdoBullet::Detector::NPlusOneQuery.update_inversed_object(self)Bullet::Detector::NPlusOneQuery.add_impossible_object(self)yield(self)ifblock_given?endendendmoduleActiveRecorddefself.enablerequire'active_record'::ActiveRecord::Base.extend(Module.newdodeffind_by_sql(sql,binds=[],preparable: nil,&block)result=superifBullet.start?ifresult.is_a?Arrayifresult.size>1Bullet::Detector::NPlusOneQuery.add_possible_objects(result)Bullet::Detector::CounterCache.add_possible_objects(result)elsifresult.size==1Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)Bullet::Detector::CounterCache.add_impossible_object(result.first)endelsifresult.is_a?::ActiveRecord::BaseBullet::Detector::NPlusOneQuery.add_impossible_object(result)Bullet::Detector::CounterCache.add_impossible_object(result)endendresultendend)::ActiveRecord::Base.prepend(SaveWithBulletSupport)::ActiveRecord::Relation.prepend(Module.newdo# if select a collection of objects, then these objects have possible to cause N+1 query.# if select only one object, then the only one object has impossible to cause N+1 query.defrecordsresult=superifBullet.start?ifresult.first.class.name!~/^HABTM_/ifresult.size>1Bullet::Detector::NPlusOneQuery.add_possible_objects(result)Bullet::Detector::CounterCache.add_possible_objects(result)elsifresult.size==1Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)Bullet::Detector::CounterCache.add_impossible_object(result.first)endendendresultendend)::ActiveRecord::Associations::Preloader.prepend(Module.newdodefpreloaders_for_one(association,records,scope)ifBullet.start?records.compact!ifrecords.first.class.name!~/^HABTM_/records.each{|record|Bullet::Detector::Association.add_object_associations(record,association)}Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records,association)endendsuperendend)::ActiveRecord::FinderMethods.prepend(Module.newdo# add includes in scopedeffind_with_associationsreturnsuper{|r|yieldr}ifblock_given?records=superifBullet.start?associations=(eager_load_values+includes_values).uniqrecords.each{|record|Bullet::Detector::Association.add_object_associations(record,associations)}Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records,associations)endrecordsendend)::ActiveRecord::Associations::JoinDependency.prepend(Module.newdoif::ActiveRecord::Associations::JoinDependency.instance_method(:instantiate).parameters.last[0]==:block# ActiveRecord >= 5.1.5definstantiate(result_set,&block)@bullet_eager_loadings={}records=superifBullet.start?@bullet_eager_loadings.eachdo|_klazz,eager_loadings_hash|objects=eager_loadings_hash.keysBullet::Detector::UnusedEagerLoading.add_eager_loadings(objects,eager_loadings_hash[objects.first].to_a)endendrecordsendelse# ActiveRecord <= 5.1.4definstantiate(result_set,aliases)@bullet_eager_loadings={}records=superifBullet.start?@bullet_eager_loadings.eachdo|_klazz,eager_loadings_hash|objects=eager_loadings_hash.keysBullet::Detector::UnusedEagerLoading.add_eager_loadings(objects,eager_loadings_hash[objects.first].to_a)endendrecordsendenddefconstruct(ar_parent,parent,row,rs,seen,model_cache,aliases)ifBullet.start?unlessar_parent.nil?parent.children.eachdo|node|key=aliases.column_alias(node,node.primary_key)id=row[key]nextunlessid.nil?associations=[node.reflection.name]ifnode.reflection.through_reflection?associations<<node.reflection.through_reflection.nameendassociations.eachdo|association|Bullet::Detector::Association.add_object_associations(ar_parent,association)Bullet::Detector::NPlusOneQuery.call_association(ar_parent,association)@bullet_eager_loadings[ar_parent.class]||={}@bullet_eager_loadings[ar_parent.class][ar_parent]||=Set.new@bullet_eager_loadings[ar_parent.class][ar_parent]<<associationendendendendsuperend# call join associationsdefconstruct_model(record,node,row,model_cache,id,aliases)result=superifBullet.start?associations=[node.reflection.name]ifnode.reflection.through_reflection?associations<<node.reflection.through_reflection.nameendassociations.eachdo|association|Bullet::Detector::Association.add_object_associations(record,association)Bullet::Detector::NPlusOneQuery.call_association(record,association)@bullet_eager_loadings[record.class]||={}@bullet_eager_loadings[record.class][record]||=Set.new@bullet_eager_loadings[record.class][record]<<associationendendresultendend)::ActiveRecord::Associations::CollectionAssociation.prepend(Module.newdodefload_targetrecords=superifBullet.start?ifis_a?::ActiveRecord::Associations::ThroughAssociationrefl=reflection.through_reflectionassociation=owner.association(refl.name)ifassociation.loaded?Bullet::Detector::NPlusOneQuery.call_association(owner,refl.name)Array.wrap(association.target).eachdo|through_record|Bullet::Detector::NPlusOneQuery.call_association(through_record,source_reflection.name)endifrefl.through_reflection?refl=refl.through_reflectionwhilerefl.through_reflection?Bullet::Detector::NPlusOneQuery.call_association(owner,refl.name)endendendBullet::Detector::NPlusOneQuery.call_association(owner,reflection.name)unless@inversedifrecords.first.class.name!~/^HABTM_/ifrecords.size>1Bullet::Detector::NPlusOneQuery.add_possible_objects(records)Bullet::Detector::CounterCache.add_possible_objects(records)elsifrecords.size==1Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)Bullet::Detector::CounterCache.add_impossible_object(records.first)endendendrecordsenddefempty?ifBullet.start?&&!reflection.has_cached_counter?Bullet::Detector::NPlusOneQuery.call_association(owner,reflection.name)endsuperenddefinclude?(object)Bullet::Detector::NPlusOneQuery.call_association(owner,reflection.name)ifBullet.start?superendend)::ActiveRecord::Associations::SingularAssociation.prepend(Module.newdo# call has_one and belongs_to associationsdeftargetresult=super()ifBullet.start?ifowner.class.name!~/^HABTM_/&&!@inversedBullet::Detector::NPlusOneQuery.call_association(owner,reflection.name)ifBullet::Detector::NPlusOneQuery.impossible?(owner)Bullet::Detector::NPlusOneQuery.add_impossible_object(result)ifresultelseBullet::Detector::NPlusOneQuery.add_possible_objects(result)ifresultendendendresultendend)::ActiveRecord::Associations::HasManyAssociation.prepend(Module.newdodefempty?result=superifBullet.start?&&!reflection.has_cached_counter?Bullet::Detector::NPlusOneQuery.call_association(owner,reflection.name)endresultenddefcount_recordsresult=reflection.has_cached_counter?ifBullet.start?&&!result&&!is_a?(::ActiveRecord::Associations::ThroughAssociation)Bullet::Detector::CounterCache.add_counter_cache(owner,reflection.name)endsuperendend)::ActiveRecord::Associations::CollectionProxy.prepend(Module.newdodefcount(column_name=nil)ifBullet.start?&&!proxy_association.is_a?(::ActiveRecord::Associations::ThroughAssociation)Bullet::Detector::CounterCache.add_counter_cache(proxy_association.owner,proxy_association.reflection.name)Bullet::Detector::NPlusOneQuery.call_association(proxy_association.owner,proxy_association.reflection.name)endsuper(column_name)endend)endendend