Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle unpersisted primary key values #448

Merged
merged 2 commits into from Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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