Skip to content

Commit

Permalink
Merge pull request #636 from camilova/master
Browse files Browse the repository at this point in the history
Refactor method to iterate fewer times over objects
  • Loading branch information
flyerhzm authored Dec 31, 2022
2 parents e88149a + cd75a78 commit 40e4abb
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/bullet/detector/n_plus_one_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,25 @@ def add_possible_objects(object_or_objects)
return unless Bullet.n_plus_one_query_enable?

objects = Array.wrap(object_or_objects)
return if objects.map(&:bullet_primary_key_value).compact.empty?
return if objects.all? { |obj| obj.class.name =~ /^HABTM_/ }

Bullet.debug(
'Detector::NPlusOneQuery#add_possible_objects',
"objects: #{objects.map(&:bullet_key).join(', ')}"
)
objects.each { |object| possible_objects.add object.bullet_key }
class_names_match_regex = true
primary_key_values_are_empty = true
keys_joined = ""
objects.each do |obj|
unless obj.class.name =~ /^HABTM_/
class_names_match_regex = false
end
unless obj.bullet_primary_key_value.nil?
primary_key_values_are_empty = false
end
keys_joined += "#{(keys_joined.empty?? '' : ', ')}#{obj.bullet_key}"
end
unless class_names_match_regex || primary_key_values_are_empty
Bullet.debug(
'Detector::NPlusOneQuery#add_possible_objects',
"objects: #{keys_joined}"
)
objects.each { |object| possible_objects.add object.bullet_key }
end
end

def add_impossible_object(object)
Expand Down

0 comments on commit 40e4abb

Please sign in to comment.