Skip to content

Commit

Permalink
Merge pull request #448 from smudge/primary-key-unpersisted
Browse files Browse the repository at this point in the history
Handle unpersisted primary key values
  • Loading branch information
flyerhzm committed Feb 22, 2019
2 parents 2ada165 + 0d5b02c commit c5f1945
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/bullet/detector/association.rb
Expand Up @@ -7,7 +7,7 @@ class <<self
def add_object_associations(object, associations)
return unless Bullet.start?
return if !Bullet.n_plus_one_query_enable? && !Bullet.unused_eager_loading_enable?
return unless object.primary_key_value
return unless object.bullet_primary_key_value

Bullet.debug('Detector::Association#add_object_associations', "object: #{object.bullet_key}, associations: #{associations}")
object_associations.add(object.bullet_key, associations)
Expand All @@ -16,7 +16,7 @@ def add_object_associations(object, associations)
def add_call_object_associations(object, associations)
return unless Bullet.start?
return if !Bullet.n_plus_one_query_enable? && !Bullet.unused_eager_loading_enable?
return unless object.primary_key_value
return unless object.bullet_primary_key_value

Bullet.debug('Detector::Association#add_call_object_associations', "object: #{object.bullet_key}, associations: #{associations}")
call_object_associations.add(object.bullet_key, associations)
Expand Down
6 changes: 3 additions & 3 deletions lib/bullet/detector/counter_cache.rb
Expand Up @@ -7,7 +7,7 @@ class <<self
def add_counter_cache(object, associations)
return unless Bullet.start?
return unless Bullet.counter_cache_enable?
return unless object.primary_key_value
return unless object.bullet_primary_key_value

Bullet.debug('Detector::CounterCache#add_counter_cache', "object: #{object.bullet_key}, associations: #{associations}")
if conditions_met?(object, associations)
Expand All @@ -20,7 +20,7 @@ def add_possible_objects(object_or_objects)
return unless Bullet.counter_cache_enable?

objects = Array(object_or_objects)
return if objects.map(&:primary_key_value).compact.empty?
return if objects.map(&:bullet_primary_key_value).compact.empty?

Bullet.debug('Detector::CounterCache#add_possible_objects', "objects: #{objects.map(&:bullet_key).join(', ')}")
objects.each { |object| possible_objects.add object.bullet_key }
Expand All @@ -29,7 +29,7 @@ def add_possible_objects(object_or_objects)
def add_impossible_object(object)
return unless Bullet.start?
return unless Bullet.counter_cache_enable?
return unless object.primary_key_value
return unless object.bullet_primary_key_value

Bullet.debug('Detector::CounterCache#add_impossible_object', "object: #{object.bullet_key}")
impossible_objects.add object.bullet_key
Expand Down
8 changes: 4 additions & 4 deletions lib/bullet/detector/n_plus_one_query.rb
Expand Up @@ -14,7 +14,7 @@ class <<self
def call_association(object, associations)
return unless Bullet.start?
return unless Bullet.n_plus_one_query_enable?
return unless object.primary_key_value
return unless object.bullet_primary_key_value
return if inversed_objects.include?(object.bullet_key, associations)

add_call_object_associations(object, associations)
Expand All @@ -31,7 +31,7 @@ def add_possible_objects(object_or_objects)
return unless Bullet.n_plus_one_query_enable?

objects = Array(object_or_objects)
return if objects.map(&:primary_key_value).compact.empty?
return if objects.map(&:bullet_primary_key_value).compact.empty?

Bullet.debug('Detector::NPlusOneQuery#add_possible_objects', "objects: #{objects.map(&:bullet_key).join(', ')}")
objects.each { |object| possible_objects.add object.bullet_key }
Expand All @@ -40,7 +40,7 @@ def add_possible_objects(object_or_objects)
def add_impossible_object(object)
return unless Bullet.start?
return unless Bullet.n_plus_one_query_enable?
return unless object.primary_key_value
return unless object.bullet_primary_key_value

Bullet.debug('Detector::NPlusOneQuery#add_impossible_object', "object: #{object.bullet_key}")
impossible_objects.add object.bullet_key
Expand All @@ -49,7 +49,7 @@ def add_impossible_object(object)
def add_inversed_object(object, association)
return unless Bullet.start?
return unless Bullet.n_plus_one_query_enable?
return unless object.primary_key_value
return unless object.bullet_primary_key_value

Bullet.debug('Detector::NPlusOneQuery#add_inversed_object', "object: #{object.bullet_key}, association: #{association}")
inversed_objects.add object.bullet_key, association
Expand Down
2 changes: 1 addition & 1 deletion lib/bullet/detector/unused_eager_loading.rb
Expand Up @@ -27,7 +27,7 @@ def check_unused_preload_associations
def add_eager_loadings(objects, associations)
return unless Bullet.start?
return unless Bullet.unused_eager_loading_enable?
return if objects.map(&:primary_key_value).compact.empty?
return if objects.map(&:bullet_primary_key_value).compact.empty?

Bullet.debug('Detector::UnusedEagerLoading#add_eager_loadings', "objects: #{objects.map(&:bullet_key).join(', ')}, associations: #{associations}")
bullet_keys = objects.map(&:bullet_key)
Expand Down
6 changes: 4 additions & 2 deletions lib/bullet/ext/object.rb
Expand Up @@ -2,10 +2,12 @@

class Object
def bullet_key
"#{self.class}:#{primary_key_value}"
"#{self.class}:#{bullet_primary_key_value}"
end

def primary_key_value
def bullet_primary_key_value
return if respond_to?(:persisted?) && !persisted?

if self.class.respond_to?(:primary_keys) && self.class.primary_keys
self.class.primary_keys.map { |primary_key| send primary_key }.join(',')
elsif self.class.respond_to?(:primary_key) && self.class.primary_key
Expand Down
13 changes: 9 additions & 4 deletions spec/bullet/ext/object_spec.rb
Expand Up @@ -17,23 +17,28 @@
end
end

context 'primary_key_value' do
context 'bullet_primary_key_value' do
it 'should return id' do
post = Post.first
expect(post.primary_key_value).to eq(post.id)
expect(post.bullet_primary_key_value).to eq(post.id)
end

it 'should return primary key value' do
post = Post.first
Post.primary_key = 'name'
expect(post.primary_key_value).to eq(post.name)
expect(post.bullet_primary_key_value).to eq(post.name)
Post.primary_key = 'id'
end

it 'should return value for multiple primary keys' do
post = Post.first
allow(Post).to receive(:primary_keys).and_return(%i[category_id writer_id])
expect(post.primary_key_value).to eq("#{post.category_id},#{post.writer_id}")
expect(post.bullet_primary_key_value).to eq("#{post.category_id},#{post.writer_id}")
end

it 'it should return nil for unpersisted records' do
post = Post.new(id: 123)
expect(post.bullet_primary_key_value).to be_nil
end
end
end

0 comments on commit c5f1945

Please sign in to comment.