# 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::Batch.prepend(Module.newdodefcallifBullet.start?@preloaders.eachdo|preloader|preloader.records.each{|record|Bullet::Detector::Association.add_object_associations(record,preloader.associations)}Bullet::Detector::UnusedEagerLoading.add_eager_loadings(preloader.records,preloader.associations)endendsuperendend)::ActiveRecord::Associations::Preloader::Branch.prepend(Module.newdodefpreloaders_for_reflection(reflection,reflection_records)ifBullet.start?reflection_records.compact!ifreflection_records.first.class.name!~/^HABTM_/reflection_records.each{|record|Bullet::Detector::Association.add_object_associations(record,reflection.name)}Bullet::Detector::UnusedEagerLoading.add_eager_loadings(reflection_records,reflection.name)endendsuperendend)::ActiveRecord::Associations::Preloader::ThroughAssociation.prepend(Module.newdodefsource_preloadersifBullet.start?&&!defined?(@source_preloaders)preloaders=superpreloaders.eachdo|preloader|reflection_name=preloader.send(:reflection).namepreloader.send(:owners).eachdo|owner|Bullet::Detector::NPlusOneQuery.call_association(owner,reflection_name)endendelsesuperendendend)::ActiveRecord::Associations::JoinDependency.prepend(Module.newdodefinstantiate(result_set,strict_loading_value,&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)endendrecordsenddefconstruct(ar_parent,parent,row,seen,model_cache,strict_loading_value)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,strict_loading_value)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::Association.prepend(Module.newdodefinversed_from(record)ifBullet.start?Bullet::Detector::NPlusOneQuery.add_inversed_object(owner,reflection.name)endsuperenddefinversed_from_queries(record)ifBullet.start?&&inversable?(record)Bullet::Detector::NPlusOneQuery.add_inversed_object(owner,reflection.name)endsuperendend)::ActiveRecord::Associations::CollectionAssociation.prepend(Module.newdodefload_targetrecords=superifBullet.start?ifis_a?::ActiveRecord::Associations::ThroughAssociationassociation=owner.association(reflection.through_reflection.name)ifassociation.loaded?Bullet::Detector::NPlusOneQuery.call_association(owner,reflection.through_reflection.name)Array.wrap(association.target).eachdo|through_record|Bullet::Detector::NPlusOneQuery.call_association(through_record,source_reflection.name)endifreflection.through_reflection!=through_reflectionBullet::Detector::NPlusOneQuery.call_association(owner,through_reflection.name)endendendBullet::Detector::NPlusOneQuery.call_association(owner,reflection.name)ifrecords.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 associationsdefreaderresult=superifBullet.start?ifowner.class.name!~/^HABTM_/ifis_a?::ActiveRecord::Associations::ThroughAssociationBullet::Detector::NPlusOneQuery.call_association(owner,reflection.through_reflection.name)association=owner.association(reflection.through_reflection.name)Array.wrap(association.target).eachdo|through_record|Bullet::Detector::NPlusOneQuery.call_association(through_record,source_reflection.name)endifreflection.through_reflection!=through_reflectionBullet::Detector::NPlusOneQuery.call_association(owner,through_reflection.name)endendBullet::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