bullet 7.0.2 → 7.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9190b7377e0184967fec123b7926cc2287755989ceea1a4c90a949721d29f411
4
- data.tar.gz: 9ae038298d2367a5862eeb6480565f9ac9d104887b97f417e390266e0bfbdb8e
3
+ metadata.gz: 1ee8344236feb882d0359202c851abb8887b6b3bb31186f2b37329a41fbc3e96
4
+ data.tar.gz: 9d1aea1b67a6777c56b2548e93614ee051d5799e8caf9ecb0d48663a6a7b5bd8
5
5
  SHA512:
6
- metadata.gz: 5b8679cc1801319b7527c472be94d75a5df7a3e03a16f2ef444d3708de584f1f8985ceedd079c35c43cc9ffb39ee5a1664b5c12d38d3d194a686692495873612
7
- data.tar.gz: 6309d7925415f5a6339ab4e180a6ef0cd3d27c3308f3a39f6bbfa120c4cb0825f6c2728365942f71e810cb967504b72ad6fa2893334adb4ce6597299cbc4abbd
6
+ metadata.gz: f603fb2540b20dd0e5d5e519de308b9a264d264bbea6db9d7399c25d6912e053aa243cb877b98cfa123f3eca9142bc0fec1bf9b4ff41f411efd6a895f616916b
7
+ data.tar.gz: 1e000b7bb96d2479108bb67bc6ac6d4efdc3f337f76f42e8227d1b8b9394a7d38d09df0104ceb3ed7b6e9a98b570dc0f32d3eeff85277490e4ee0c552b577f5b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## Next Release
2
2
 
3
+ ## 7.0.4 (11/28/2022)
4
+
5
+ * Fix `eager_load` `has_many :through` false positives
6
+ * mongoid7x: add dynamic methods
7
+
8
+ ## 7.0.3 (08/13/2022)
9
+
10
+ * Replace `Array()` with `Array.wrap()`
11
+
3
12
  ## 7.0.2 (05/31/2022)
4
13
 
5
14
  * Drop growl support
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 - 2010 Richard Huang (flyerhzm@gmail.com)
1
+ Copyright (c) 2009 - 2022 Richard Huang (flyerhzm@gmail.com)
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -285,7 +285,7 @@ $ rails g scaffold comment name:string post_id:integer
285
285
  $ bundle exec rake db:migrate
286
286
  ```
287
287
 
288
- 2\. Change `app/model/post.rb` and `app/model/comment.rb`
288
+ 2\. Change `app/models/post.rb` and `app/models/comment.rb`
289
289
 
290
290
  ```ruby
291
291
  class Post < ActiveRecord::Base
@@ -177,10 +177,12 @@ module Bullet
177
177
  if Bullet.start?
178
178
  if is_a? ::ActiveRecord::Associations::ThroughAssociation
179
179
  refl = reflection.through_reflection
180
- Bullet::Detector::NPlusOneQuery.call_association(owner, refl.name)
181
- association = owner.association refl.name
182
- Array(association.target).each do |through_record|
183
- Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
180
+ association = owner.association(refl.name)
181
+ if association.loaded?
182
+ Bullet::Detector::NPlusOneQuery.call_association(owner, refl.name)
183
+ Array.wrap(association.target).each do |through_record|
184
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
185
+ end
184
186
  end
185
187
 
186
188
  if refl.through_reflection?
@@ -150,10 +150,12 @@ module Bullet
150
150
 
151
151
  if Bullet.start?
152
152
  if is_a? ::ActiveRecord::Associations::ThroughAssociation
153
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
154
- association = owner.association reflection.through_reflection.name
155
- Array(association.target).each do |through_record|
156
- Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
153
+ association = owner.association(reflection.through_reflection.name)
154
+ if association.loaded?
155
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
156
+ Array.wrap(association.target).each do |through_record|
157
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
158
+ end
157
159
  end
158
160
 
159
161
  if reflection.through_reflection != through_reflection
@@ -199,7 +201,7 @@ module Bullet
199
201
  if is_a? ::ActiveRecord::Associations::ThroughAssociation
200
202
  Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
201
203
  association = owner.association reflection.through_reflection.name
202
- Array(association.target).each do |through_record|
204
+ Array.wrap(association.target).each do |through_record|
203
205
  Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
204
206
  end
205
207
 
@@ -177,10 +177,12 @@ module Bullet
177
177
 
178
178
  if Bullet.start?
179
179
  if is_a? ::ActiveRecord::Associations::ThroughAssociation
180
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
181
180
  association = owner.association(reflection.through_reflection.name)
182
- Array(association.target).each do |through_record|
183
- Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
181
+ if association.loaded?
182
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
183
+ Array.wrap(association.target).each do |through_record|
184
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
185
+ end
184
186
  end
185
187
 
186
188
  if reflection.through_reflection != through_reflection
@@ -226,7 +228,7 @@ module Bullet
226
228
  if is_a? ::ActiveRecord::Associations::ThroughAssociation
227
229
  Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
228
230
  association = owner.association(reflection.through_reflection.name)
229
- Array(association.target).each do |through_record|
231
+ Array.wrap(association.target).each do |through_record|
230
232
  Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
231
233
  end
232
234
 
@@ -177,10 +177,12 @@ module Bullet
177
177
 
178
178
  if Bullet.start?
179
179
  if is_a? ::ActiveRecord::Associations::ThroughAssociation
180
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
181
180
  association = owner.association(reflection.through_reflection.name)
182
- Array(association.target).each do |through_record|
183
- Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
181
+ if association.loaded?
182
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
183
+ Array.wrap(association.target).each do |through_record|
184
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
185
+ end
184
186
  end
185
187
 
186
188
  if reflection.through_reflection != through_reflection
@@ -226,7 +228,7 @@ module Bullet
226
228
  if is_a? ::ActiveRecord::Associations::ThroughAssociation
227
229
  Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
228
230
  association = owner.association(reflection.through_reflection.name)
229
- Array(association.target).each do |through_record|
231
+ Array.wrap(association.target).each do |through_record|
230
232
  Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
231
233
  end
232
234
 
@@ -180,10 +180,12 @@ module Bullet
180
180
 
181
181
  if Bullet.start?
182
182
  if is_a? ::ActiveRecord::Associations::ThroughAssociation
183
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
184
183
  association = owner.association(reflection.through_reflection.name)
185
- Array(association.target).each do |through_record|
186
- Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
184
+ if association.loaded?
185
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
186
+ Array.wrap(association.target).each do |through_record|
187
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
188
+ end
187
189
  end
188
190
 
189
191
  if reflection.through_reflection != through_reflection
@@ -229,7 +231,7 @@ module Bullet
229
231
  if is_a? ::ActiveRecord::Associations::ThroughAssociation
230
232
  Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
231
233
  association = owner.association(reflection.through_reflection.name)
232
- Array(association.target).each do |through_record|
234
+ Array.wrap(association.target).each do |through_record|
233
235
  Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
234
236
  end
235
237
 
@@ -20,7 +20,7 @@ module Bullet
20
20
  return unless Bullet.start?
21
21
  return unless Bullet.counter_cache_enable?
22
22
 
23
- objects = Array(object_or_objects)
23
+ objects = Array.wrap(object_or_objects)
24
24
  return if objects.map(&:bullet_primary_key_value).compact.empty?
25
25
 
26
26
  Bullet.debug(
@@ -54,7 +54,7 @@ module Bullet
54
54
  private
55
55
 
56
56
  def create_notification(klazz, associations)
57
- notify_associations = Array(associations) - Bullet.get_safelist_associations(:counter_cache, klazz)
57
+ notify_associations = Array.wrap(associations) - Bullet.get_safelist_associations(:counter_cache, klazz)
58
58
 
59
59
  if notify_associations.present?
60
60
  notice = Bullet::Notification::CounterCache.new klazz, notify_associations
@@ -7,7 +7,7 @@ module Bullet
7
7
  extend StackTraceFilter
8
8
 
9
9
  class << self
10
- # executed when object.assocations is called.
10
+ # executed when object.associations is called.
11
11
  # first, it keeps this method call for object.association.
12
12
  # then, it checks if this associations call is unpreload.
13
13
  # if it is, keeps this unpreload associations and caller.
@@ -33,7 +33,7 @@ module Bullet
33
33
  return unless Bullet.start?
34
34
  return unless Bullet.n_plus_one_query_enable?
35
35
 
36
- objects = Array(object_or_objects)
36
+ objects = Array.wrap(object_or_objects)
37
37
  return if objects.map(&:bullet_primary_key_value).compact.empty?
38
38
  return if objects.all? { |obj| obj.class.name =~ /^HABTM_/ }
39
39
 
@@ -95,7 +95,7 @@ module Bullet
95
95
  private
96
96
 
97
97
  def create_notification(callers, klazz, associations)
98
- notify_associations = Array(associations) - Bullet.get_safelist_associations(:n_plus_one_query, klazz)
98
+ notify_associations = Array.wrap(associations) - Bullet.get_safelist_associations(:n_plus_one_query, klazz)
99
99
 
100
100
  if notify_associations.present?
101
101
  notice = Bullet::Notification::NPlusOneQuery.new(callers, klazz, notify_associations)
@@ -10,7 +10,7 @@ module Bullet
10
10
  # check if there are unused preload associations.
11
11
  # get related_objects from eager_loadings associated with object and associations
12
12
  # get call_object_association from associations of call_object_associations whose object is in related_objects
13
- # if association not in call_object_association, then the object => association - call_object_association is ununsed preload assocations
13
+ # if association not in call_object_association, then the object => association - call_object_association is ununsed preload associations
14
14
  def check_unused_preload_associations
15
15
  return unless Bullet.start?
16
16
  return unless Bullet.unused_eager_loading_enable?
@@ -65,7 +65,7 @@ module Bullet
65
65
  private
66
66
 
67
67
  def create_notification(callers, klazz, associations)
68
- notify_associations = Array(associations) - Bullet.get_safelist_associations(:unused_eager_loading, klazz)
68
+ notify_associations = Array.wrap(associations) - Bullet.get_safelist_associations(:unused_eager_loading, klazz)
69
69
 
70
70
  if notify_associations.present?
71
71
  notice = Bullet::Notification::UnusedEagerLoading.new(callers, klazz, notify_associations)
@@ -4,22 +4,20 @@ module Bullet
4
4
  module Mongoid
5
5
  def self.enable
6
6
  require 'mongoid'
7
+ require 'rubygems'
7
8
  ::Mongoid::Contextual::Mongo.class_eval do
8
9
  alias_method :origin_first, :first
9
10
  alias_method :origin_last, :last
10
11
  alias_method :origin_each, :each
11
12
  alias_method :origin_eager_load, :eager_load
12
13
 
13
- def first(opts = {})
14
- result = origin_first(opts)
15
- Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
16
- result
17
- end
18
-
19
- def last(opts = {})
20
- result = origin_last(opts)
21
- Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
22
- result
14
+ %i[first last].each do |context|
15
+ default = Gem::Version.new(::Mongoid::VERSION) >= Gem::Version.new('7.5') ? nil : {}
16
+ define_method(context) do |opts = default|
17
+ result = send(:"origin_#{context}", opts)
18
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
19
+ result
20
+ end
23
21
  end
24
22
 
25
23
  def each(&block)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bullet
4
- VERSION = '7.0.2'
4
+ VERSION = '7.0.4'
5
5
  end
data/lib/bullet.rb CHANGED
@@ -109,7 +109,7 @@ module Bullet
109
109
  end
110
110
 
111
111
  def get_safelist_associations(type, class_name)
112
- Array(@safelist[type][class_name])
112
+ Array.wrap(@safelist[type][class_name])
113
113
  end
114
114
 
115
115
  def reset_safelist
@@ -39,7 +39,7 @@ module Bullet
39
39
 
40
40
  it 'should be false if object, association pair is not existed' do
41
41
  NPlusOneQuery.add_object_associations(@post, :association1)
42
- expect(NPlusOneQuery.association?(@post, :associatio2)).to eq false
42
+ expect(NPlusOneQuery.association?(@post, :association2)).to eq false
43
43
  end
44
44
  end
45
45
 
@@ -474,7 +474,15 @@ if active_record?
474
474
  end
475
475
 
476
476
  it 'should detect preload associations' do
477
- Firm.includes(:clients).each { |firm| firm.clients.map(&:name) }
477
+ Firm.preload(:clients).each { |firm| firm.clients.map(&:name) }
478
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
479
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
480
+
481
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
482
+ end
483
+
484
+ it 'should detect eager load association' do
485
+ Firm.eager_load(:clients).each { |firm| firm.clients.map(&:name) }
478
486
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
479
487
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
480
488
 
@@ -92,7 +92,7 @@ module Support
92
92
  page3 = Page.create(name: 'page3', parent_id: folder2.id, author_id: author2.id)
93
93
  page4 = Page.create(name: 'page4', parent_id: folder2.id, author_id: author2.id)
94
94
 
95
- role1 = Role.create(name: 'Amdin')
95
+ role1 = Role.create(name: 'Admin')
96
96
  role2 = Role.create(name: 'User')
97
97
 
98
98
  user1 = User.create(name: 'user1', category: category1)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bullet
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.2
4
+ version: 7.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-31 00:00:00.000000000 Z
11
+ date: 2022-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -195,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
195
  - !ruby/object:Gem::Version
196
196
  version: 1.3.6
197
197
  requirements: []
198
- rubygems_version: 3.3.7
198
+ rubygems_version: 3.3.22
199
199
  signing_key:
200
200
  specification_version: 4
201
201
  summary: help to kill N+1 queries and unused eager loading.