bullet 6.1.3 → 7.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +82 -0
- data/CHANGELOG.md +36 -0
- data/Gemfile.rails-7.0 +10 -0
- data/MIT-LICENSE +1 -1
- data/README.md +31 -26
- data/lib/bullet/active_record41.rb +1 -0
- data/lib/bullet/active_record42.rb +1 -0
- data/lib/bullet/active_record5.rb +6 -4
- data/lib/bullet/active_record52.rb +18 -22
- data/lib/bullet/active_record60.rb +17 -21
- data/lib/bullet/active_record61.rb +17 -21
- data/lib/bullet/active_record70.rb +277 -0
- data/lib/bullet/bullet_xhr.js +3 -2
- data/lib/bullet/dependency.rb +10 -0
- data/lib/bullet/detector/base.rb +2 -1
- data/lib/bullet/detector/counter_cache.rb +2 -2
- data/lib/bullet/detector/n_plus_one_query.rb +5 -5
- data/lib/bullet/detector/unused_eager_loading.rb +2 -2
- data/lib/bullet/mongoid7x.rb +34 -19
- data/lib/bullet/notification.rb +2 -1
- data/lib/bullet/rack.rb +7 -4
- data/lib/bullet/stack_trace_filter.rb +7 -8
- data/lib/bullet/version.rb +1 -1
- data/lib/bullet.rb +23 -24
- data/lib/generators/bullet/install_generator.rb +0 -1
- data/perf/benchmark.rb +4 -1
- data/spec/bullet/detector/n_plus_one_query_spec.rb +1 -1
- data/spec/bullet/detector/unused_eager_loading_spec.rb +6 -2
- data/spec/bullet/ext/object_spec.rb +1 -1
- data/spec/bullet/notification/base_spec.rb +4 -4
- data/spec/bullet/rack_spec.rb +90 -19
- data/spec/bullet_spec.rb +15 -15
- data/spec/integration/active_record/association_spec.rb +36 -9
- data/spec/integration/counter_cache_spec.rb +4 -4
- data/spec/integration/mongoid/association_spec.rb +1 -1
- data/spec/models/deal.rb +5 -0
- data/spec/models/folder.rb +2 -1
- data/spec/models/group.rb +2 -1
- data/spec/models/page.rb +2 -1
- data/spec/models/post.rb +2 -0
- data/spec/models/role.rb +7 -0
- data/spec/models/user.rb +1 -0
- data/spec/models/writer.rb +2 -1
- data/spec/spec_helper.rb +0 -2
- data/spec/support/mongo_seed.rb +1 -0
- data/spec/support/sqlite_seed.rb +30 -0
- data/test.sh +2 -0
- metadata +10 -4
- data/.travis.yml +0 -33
data/lib/bullet.rb
CHANGED
@@ -20,9 +20,6 @@ module Bullet
|
|
20
20
|
autoload :Registry, 'bullet/registry'
|
21
21
|
autoload :NotificationCollector, 'bullet/notification_collector'
|
22
22
|
|
23
|
-
BULLET_DEBUG = 'BULLET_DEBUG'
|
24
|
-
TRUE = 'true'
|
25
|
-
|
26
23
|
if defined?(Rails::Railtie)
|
27
24
|
class BulletRailtie < Rails::Railtie
|
28
25
|
initializer 'bullet.configure_rails_initialization' do |app|
|
@@ -38,10 +35,11 @@ module Bullet
|
|
38
35
|
:stacktrace_includes,
|
39
36
|
:stacktrace_excludes,
|
40
37
|
:skip_html_injection
|
41
|
-
attr_reader :
|
42
|
-
attr_accessor :add_footer, :orm_patches_applied
|
38
|
+
attr_reader :safelist
|
39
|
+
attr_accessor :add_footer, :orm_patches_applied, :skip_http_headers
|
43
40
|
|
44
|
-
available_notifiers =
|
41
|
+
available_notifiers =
|
42
|
+
UniformNotifier::AVAILABLE_NOTIFIERS.select { |notifier| notifier != :raise }.map { |notifier| "#{notifier}=" }
|
45
43
|
available_notifiers_options = { to: UniformNotifier }
|
46
44
|
delegate(*available_notifiers, **available_notifiers_options)
|
47
45
|
|
@@ -59,7 +57,7 @@ module Bullet
|
|
59
57
|
@enable = @n_plus_one_query_enable = @unused_eager_loading_enable = @counter_cache_enable = enable
|
60
58
|
|
61
59
|
if enable?
|
62
|
-
|
60
|
+
reset_safelist
|
63
61
|
unless orm_patches_applied
|
64
62
|
self.orm_patches_applied = true
|
65
63
|
Bullet::Mongoid.enable if mongoid?
|
@@ -72,8 +70,9 @@ module Bullet
|
|
72
70
|
!!@enable
|
73
71
|
end
|
74
72
|
|
73
|
+
# Rails.root might be nil if `railties` is a dependency on a project that does not use Rails
|
75
74
|
def app_root
|
76
|
-
(defined?(::Rails.root) ? Rails.root.to_s : Dir.pwd).to_s
|
75
|
+
@app_root ||= (defined?(::Rails.root) && !::Rails.root.nil? ? Rails.root.to_s : Dir.pwd).to_s
|
77
76
|
end
|
78
77
|
|
79
78
|
def n_plus_one_query_enable?
|
@@ -96,29 +95,29 @@ module Bullet
|
|
96
95
|
@stacktrace_excludes ||= []
|
97
96
|
end
|
98
97
|
|
99
|
-
def
|
100
|
-
|
101
|
-
@
|
102
|
-
@
|
98
|
+
def add_safelist(options)
|
99
|
+
reset_safelist
|
100
|
+
@safelist[options[:type]][options[:class_name]] ||= []
|
101
|
+
@safelist[options[:type]][options[:class_name]] << options[:association].to_sym
|
103
102
|
end
|
104
103
|
|
105
|
-
def
|
106
|
-
|
107
|
-
@
|
108
|
-
@
|
109
|
-
@
|
104
|
+
def delete_safelist(options)
|
105
|
+
reset_safelist
|
106
|
+
@safelist[options[:type]][options[:class_name]] ||= []
|
107
|
+
@safelist[options[:type]][options[:class_name]].delete(options[:association].to_sym)
|
108
|
+
@safelist[options[:type]].delete_if { |_key, val| val.empty? }
|
110
109
|
end
|
111
110
|
|
112
|
-
def
|
113
|
-
Array(@
|
111
|
+
def get_safelist_associations(type, class_name)
|
112
|
+
Array.wrap(@safelist[type][class_name])
|
114
113
|
end
|
115
114
|
|
116
|
-
def
|
117
|
-
@
|
115
|
+
def reset_safelist
|
116
|
+
@safelist ||= { n_plus_one_query: {}, unused_eager_loading: {}, counter_cache: {} }
|
118
117
|
end
|
119
118
|
|
120
|
-
def
|
121
|
-
@
|
119
|
+
def clear_safelist
|
120
|
+
@safelist = nil
|
122
121
|
end
|
123
122
|
|
124
123
|
def bullet_logger=(active)
|
@@ -132,7 +131,7 @@ module Bullet
|
|
132
131
|
end
|
133
132
|
|
134
133
|
def debug(title, message)
|
135
|
-
puts "[Bullet][#{title}] #{message}" if ENV[BULLET_DEBUG] ==
|
134
|
+
puts "[Bullet][#{title}] #{message}" if ENV['BULLET_DEBUG'] == 'true'
|
136
135
|
end
|
137
136
|
|
138
137
|
def start_request
|
data/perf/benchmark.rb
CHANGED
@@ -30,7 +30,10 @@ end
|
|
30
30
|
|
31
31
|
# create database bullet_benchmark;
|
32
32
|
ActiveRecord::Base.establish_connection(
|
33
|
-
adapter: 'mysql2',
|
33
|
+
adapter: 'mysql2',
|
34
|
+
database: 'bullet_benchmark',
|
35
|
+
server: '/tmp/mysql.socket',
|
36
|
+
username: 'root'
|
34
37
|
)
|
35
38
|
|
36
39
|
ActiveRecord::Base.connection.tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
|
@@ -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, :
|
42
|
+
expect(NPlusOneQuery.association?(@post, :association2)).to eq false
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -19,7 +19,9 @@ module Bullet
|
|
19
19
|
it 'should get call associations if object and association are both in eager_loadings and call_object_associations' do
|
20
20
|
UnusedEagerLoading.add_eager_loadings([@post], :association)
|
21
21
|
UnusedEagerLoading.add_call_object_associations(@post, :association)
|
22
|
-
expect(UnusedEagerLoading.send(:call_associations, @post.bullet_key, Set.new([:association]))).to eq(
|
22
|
+
expect(UnusedEagerLoading.send(:call_associations, @post.bullet_key, Set.new([:association]))).to eq(
|
23
|
+
[:association]
|
24
|
+
)
|
23
25
|
end
|
24
26
|
|
25
27
|
it 'should not get call associations if not exist in call_object_associations' do
|
@@ -30,7 +32,9 @@ module Bullet
|
|
30
32
|
|
31
33
|
context '.diff_object_associations' do
|
32
34
|
it 'should return associations not exist in call_association' do
|
33
|
-
expect(UnusedEagerLoading.send(:diff_object_associations, @post.bullet_key, Set.new([:association]))).to eq(
|
35
|
+
expect(UnusedEagerLoading.send(:diff_object_associations, @post.bullet_key, Set.new([:association]))).to eq(
|
36
|
+
[:association]
|
37
|
+
)
|
34
38
|
end
|
35
39
|
|
36
40
|
it 'should return empty if associations exist in call_association' do
|
@@ -10,7 +10,7 @@ describe Object do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
if mongoid?
|
13
|
-
it 'should return class with
|
13
|
+
it 'should return class with namespace and id composition' do
|
14
14
|
post = Mongoid::Post.first
|
15
15
|
expect(post.bullet_key).to eq("Mongoid::Post:#{post.id}")
|
16
16
|
end
|
@@ -74,8 +74,8 @@ module Bullet
|
|
74
74
|
it 'should send full_notice to notifier' do
|
75
75
|
notifier = double
|
76
76
|
allow(subject).to receive(:notifier).and_return(notifier)
|
77
|
-
allow(subject).to receive(:notification_data).and_return(foo: :bar)
|
78
|
-
expect(notifier).to receive(:inline_notify).with(foo: :bar)
|
77
|
+
allow(subject).to receive(:notification_data).and_return({ foo: :bar })
|
78
|
+
expect(notifier).to receive(:inline_notify).with({ foo: :bar })
|
79
79
|
subject.notify_inline
|
80
80
|
end
|
81
81
|
end
|
@@ -84,8 +84,8 @@ module Bullet
|
|
84
84
|
it 'should send full_out_of_channel to notifier' do
|
85
85
|
notifier = double
|
86
86
|
allow(subject).to receive(:notifier).and_return(notifier)
|
87
|
-
allow(subject).to receive(:notification_data).and_return(foo: :bar)
|
88
|
-
expect(notifier).to receive(:out_of_channel_notify).with(foo: :bar)
|
87
|
+
allow(subject).to receive(:notification_data).and_return({ foo: :bar })
|
88
|
+
expect(notifier).to receive(:out_of_channel_notify).with({ foo: :bar })
|
89
89
|
subject.notify_out_of_channel
|
90
90
|
end
|
91
91
|
end
|
data/spec/bullet/rack_spec.rb
CHANGED
@@ -31,12 +31,6 @@ module Bullet
|
|
31
31
|
response = double(body: '<html><head></head><body></body></html>')
|
32
32
|
expect(middleware).not_to be_html_request(headers, response)
|
33
33
|
end
|
34
|
-
|
35
|
-
it "should be false if response body doesn't contain html tag" do
|
36
|
-
headers = { 'Content-Type' => 'text/html' }
|
37
|
-
response = double(body: '<div>Partial</div>')
|
38
|
-
expect(middleware).not_to be_html_request(headers, response)
|
39
|
-
end
|
40
34
|
end
|
41
35
|
|
42
36
|
context 'empty?' do
|
@@ -54,6 +48,11 @@ module Bullet
|
|
54
48
|
response = double(body: '')
|
55
49
|
expect(middleware).to be_empty(response)
|
56
50
|
end
|
51
|
+
|
52
|
+
it 'should be true if no response body' do
|
53
|
+
response = double()
|
54
|
+
expect(middleware).to be_empty(response)
|
55
|
+
end
|
57
56
|
end
|
58
57
|
|
59
58
|
context '#call' do
|
@@ -90,7 +89,7 @@ module Bullet
|
|
90
89
|
before do
|
91
90
|
expect(Bullet).to receive(:notification?).and_return(true)
|
92
91
|
allow(Bullet).to receive(:gather_inline_notifications).and_return('<bullet></bullet>')
|
93
|
-
allow(middleware).to receive(:xhr_script).and_return('')
|
92
|
+
allow(middleware).to receive(:xhr_script).and_return('<script></script>')
|
94
93
|
allow(middleware).to receive(:footer_note).and_return('footer')
|
95
94
|
expect(Bullet).to receive(:perform_out_of_channel_notifications)
|
96
95
|
end
|
@@ -99,21 +98,28 @@ module Bullet
|
|
99
98
|
expect(Bullet).to receive(:add_footer).exactly(3).times.and_return(true)
|
100
99
|
_, headers, response = middleware.call('Content-Type' => 'text/html')
|
101
100
|
|
102
|
-
expect(headers['Content-Length']).to eq((
|
103
|
-
expect(response
|
104
|
-
expect(response.first).to include('<bullet></bullet><')
|
101
|
+
expect(headers['Content-Length']).to eq((73 + middleware.send(:footer_note).length).to_s)
|
102
|
+
expect(response).to eq(%w[<html><head></head><body>footer<bullet></bullet><script></script></body></html>])
|
105
103
|
end
|
106
104
|
|
107
105
|
it 'should change response body for html safe string if add_footer is true' do
|
108
106
|
expect(Bullet).to receive(:add_footer).exactly(3).times.and_return(true)
|
109
|
-
app.response =
|
110
|
-
|
111
|
-
|
107
|
+
app.response =
|
108
|
+
Support::ResponseDouble.new.tap do |response|
|
109
|
+
response.body = ActiveSupport::SafeBuffer.new('<html><head></head><body></body></html>')
|
110
|
+
end
|
112
111
|
_, headers, response = middleware.call('Content-Type' => 'text/html')
|
113
112
|
|
114
|
-
expect(headers['Content-Length']).to eq((
|
115
|
-
expect(response
|
116
|
-
|
113
|
+
expect(headers['Content-Length']).to eq((73 + middleware.send(:footer_note).length).to_s)
|
114
|
+
expect(response).to eq(%w[<html><head></head><body>footer<bullet></bullet><script></script></body></html>])
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should add the footer-text header for non-html requests when add_footer is true' do
|
118
|
+
allow(Bullet).to receive(:add_footer).at_least(:once).and_return(true)
|
119
|
+
allow(Bullet).to receive(:footer_info).and_return(['footer text'])
|
120
|
+
app.headers = { 'Content-Type' => 'application/json' }
|
121
|
+
_, headers, _response = middleware.call({})
|
122
|
+
expect(headers).to include('X-bullet-footer-text' => '["footer text"]')
|
117
123
|
end
|
118
124
|
|
119
125
|
it 'should change response body if console_enabled is true' do
|
@@ -125,20 +131,71 @@ module Bullet
|
|
125
131
|
|
126
132
|
it 'should change response body for html safe string if console_enabled is true' do
|
127
133
|
expect(Bullet).to receive(:console_enabled?).and_return(true)
|
128
|
-
app.response =
|
129
|
-
|
130
|
-
|
134
|
+
app.response =
|
135
|
+
Support::ResponseDouble.new.tap do |response|
|
136
|
+
response.body = ActiveSupport::SafeBuffer.new('<html><head></head><body></body></html>')
|
137
|
+
end
|
131
138
|
_, headers, response = middleware.call('Content-Type' => 'text/html')
|
132
139
|
expect(headers['Content-Length']).to eq('56')
|
133
140
|
expect(response).to eq(%w[<html><head></head><body><bullet></bullet></body></html>])
|
134
141
|
end
|
135
142
|
|
143
|
+
it 'should add headers for non-html requests when console_enabled is true' do
|
144
|
+
allow(Bullet).to receive(:console_enabled?).at_least(:once).and_return(true)
|
145
|
+
allow(Bullet).to receive(:text_notifications).and_return(['text notifications'])
|
146
|
+
app.headers = { 'Content-Type' => 'application/json' }
|
147
|
+
_, headers, _response = middleware.call({})
|
148
|
+
expect(headers).to include('X-bullet-console-text' => '["text notifications"]')
|
149
|
+
end
|
150
|
+
|
136
151
|
it "shouldn't change response body unnecessarily" do
|
137
152
|
expected_response = Support::ResponseDouble.new 'Actual body'
|
138
153
|
app.response = expected_response
|
139
154
|
_, _, response = middleware.call({})
|
140
155
|
expect(response).to eq(expected_response)
|
141
156
|
end
|
157
|
+
|
158
|
+
it "shouldn't add headers unnecessarily" do
|
159
|
+
app.headers = { 'Content-Type' => 'application/json' }
|
160
|
+
_, headers, _response = middleware.call({})
|
161
|
+
expect(headers).not_to include('X-bullet-footer-text')
|
162
|
+
expect(headers).not_to include('X-bullet-console-text')
|
163
|
+
end
|
164
|
+
|
165
|
+
context 'when skip_http_headers is enabled' do
|
166
|
+
before do
|
167
|
+
allow(Bullet).to receive(:skip_http_headers).and_return(true)
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'should include the footer but not the xhr script tag if add_footer is true' do
|
171
|
+
expect(Bullet).to receive(:add_footer).at_least(:once).and_return(true)
|
172
|
+
_, headers, response = middleware.call({})
|
173
|
+
|
174
|
+
expect(headers['Content-Length']).to eq((56 + middleware.send(:footer_note).length).to_s)
|
175
|
+
expect(response).to eq(%w[<html><head></head><body>footer<bullet></bullet></body></html>])
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'should not include the xhr script tag if console_enabled is true' do
|
179
|
+
expect(Bullet).to receive(:console_enabled?).and_return(true)
|
180
|
+
_, headers, response = middleware.call({})
|
181
|
+
expect(headers['Content-Length']).to eq('56')
|
182
|
+
expect(response).to eq(%w[<html><head></head><body><bullet></bullet></body></html>])
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'should not add the footer-text header for non-html requests when add_footer is true' do
|
186
|
+
allow(Bullet).to receive(:add_footer).at_least(:once).and_return(true)
|
187
|
+
app.headers = { 'Content-Type' => 'application/json' }
|
188
|
+
_, headers, _response = middleware.call({})
|
189
|
+
expect(headers).not_to include('X-bullet-footer-text')
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'should not add headers for non-html requests when console_enabled is true' do
|
193
|
+
allow(Bullet).to receive(:console_enabled?).at_least(:once).and_return(true)
|
194
|
+
app.headers = { 'Content-Type' => 'application/json' }
|
195
|
+
_, headers, _response = middleware.call({})
|
196
|
+
expect(headers).not_to include('X-bullet-console-text')
|
197
|
+
end
|
198
|
+
end
|
142
199
|
end
|
143
200
|
|
144
201
|
context 'when skip_html_injection is enabled' do
|
@@ -203,6 +260,20 @@ module Bullet
|
|
203
260
|
expect(middleware.response_body(response)).to eq body_string
|
204
261
|
end
|
205
262
|
end
|
263
|
+
|
264
|
+
begin
|
265
|
+
require 'rack/files'
|
266
|
+
|
267
|
+
context 'when `response` is a Rack::Files::Iterator' do
|
268
|
+
let(:response) { instance_double(::Rack::Files::Iterator) }
|
269
|
+
before { allow(response).to receive(:is_a?).with(::Rack::Files::Iterator) { true } }
|
270
|
+
|
271
|
+
it 'should return nil' do
|
272
|
+
expect(middleware.response_body(response)).to be_nil
|
273
|
+
end
|
274
|
+
end
|
275
|
+
rescue LoadError
|
276
|
+
end
|
206
277
|
end
|
207
278
|
end
|
208
279
|
end
|
data/spec/bullet_spec.rb
CHANGED
@@ -74,31 +74,31 @@ describe Bullet, focused: true do
|
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
|
-
describe '#
|
77
|
+
describe '#add_safelist' do
|
78
78
|
context "for 'special' class names" do
|
79
|
-
it 'is added to the
|
80
|
-
Bullet.
|
81
|
-
expect(Bullet.
|
79
|
+
it 'is added to the safelist successfully' do
|
80
|
+
Bullet.add_safelist(type: :n_plus_one_query, class_name: 'Klass', association: :department)
|
81
|
+
expect(Bullet.get_safelist_associations(:n_plus_one_query, 'Klass')).to include :department
|
82
82
|
end
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
describe '#
|
86
|
+
describe '#delete_safelist' do
|
87
87
|
context "for 'special' class names" do
|
88
|
-
it 'is deleted from the
|
89
|
-
Bullet.
|
90
|
-
Bullet.
|
91
|
-
expect(Bullet.
|
88
|
+
it 'is deleted from the safelist successfully' do
|
89
|
+
Bullet.add_safelist(type: :n_plus_one_query, class_name: 'Klass', association: :department)
|
90
|
+
Bullet.delete_safelist(type: :n_plus_one_query, class_name: 'Klass', association: :department)
|
91
|
+
expect(Bullet.safelist[:n_plus_one_query]).to eq({})
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
95
|
context 'when exists multiple definitions' do
|
96
|
-
it 'is deleted from the
|
97
|
-
Bullet.
|
98
|
-
Bullet.
|
99
|
-
Bullet.
|
100
|
-
expect(Bullet.
|
101
|
-
expect(Bullet.
|
96
|
+
it 'is deleted from the safelist successfully' do
|
97
|
+
Bullet.add_safelist(type: :n_plus_one_query, class_name: 'Klass', association: :department)
|
98
|
+
Bullet.add_safelist(type: :n_plus_one_query, class_name: 'Klass', association: :team)
|
99
|
+
Bullet.delete_safelist(type: :n_plus_one_query, class_name: 'Klass', association: :team)
|
100
|
+
expect(Bullet.get_safelist_associations(:n_plus_one_query, 'Klass')).to include :department
|
101
|
+
expect(Bullet.get_safelist_associations(:n_plus_one_query, 'Klass')).to_not include :team
|
102
102
|
end
|
103
103
|
end
|
104
104
|
end
|
@@ -129,7 +129,7 @@ if active_record?
|
|
129
129
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
130
130
|
end
|
131
131
|
|
132
|
-
it 'should detect unused preload with post =>
|
132
|
+
it 'should detect unused preload with post => comments, no category => posts' do
|
133
133
|
Category.includes(posts: :comments).each { |category| category.posts.map(&:name) }
|
134
134
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
135
135
|
expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
|
@@ -202,7 +202,7 @@ if active_record?
|
|
202
202
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
203
203
|
end
|
204
204
|
|
205
|
-
it 'should detect preload with post =>
|
205
|
+
it 'should detect preload with post => comments' do
|
206
206
|
Post.first.comments.map(&:name)
|
207
207
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
208
208
|
expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
|
@@ -401,6 +401,15 @@ if active_record?
|
|
401
401
|
end
|
402
402
|
|
403
403
|
describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
|
404
|
+
context 'posts <=> deals' do
|
405
|
+
it 'should detect preload associations with join tables that have identifier' do
|
406
|
+
Post.includes(:deals).each { |post| post.deals.map(&:name) }
|
407
|
+
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
408
|
+
expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
|
409
|
+
|
410
|
+
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
411
|
+
end
|
412
|
+
end
|
404
413
|
context 'students <=> teachers' do
|
405
414
|
it 'should detect non preload associations' do
|
406
415
|
Student.all.each { |student| student.teachers.map(&:name) }
|
@@ -442,6 +451,16 @@ if active_record?
|
|
442
451
|
expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Student, :teachers)
|
443
452
|
end
|
444
453
|
end
|
454
|
+
|
455
|
+
context 'user => roles' do
|
456
|
+
it 'should detect preload associations' do
|
457
|
+
User.first.roles.includes(:resource).each { |role| role.resource }
|
458
|
+
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
459
|
+
expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
|
460
|
+
|
461
|
+
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
462
|
+
end
|
463
|
+
end
|
445
464
|
end
|
446
465
|
|
447
466
|
describe Bullet::Detector::Association, 'has_many :through' do
|
@@ -455,7 +474,15 @@ if active_record?
|
|
455
474
|
end
|
456
475
|
|
457
476
|
it 'should detect preload associations' do
|
458
|
-
Firm.
|
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) }
|
459
486
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
460
487
|
expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
|
461
488
|
|
@@ -729,9 +756,9 @@ if active_record?
|
|
729
756
|
end
|
730
757
|
end
|
731
758
|
|
732
|
-
context '
|
733
|
-
before { Bullet.
|
734
|
-
after { Bullet.
|
759
|
+
context 'add n plus one query to safelist' do
|
760
|
+
before { Bullet.add_safelist type: :n_plus_one_query, class_name: 'Post', association: :comments }
|
761
|
+
after { Bullet.clear_safelist }
|
735
762
|
|
736
763
|
it 'should not detect n plus one query' do
|
737
764
|
Post.all.each { |post| post.comments.map(&:name) }
|
@@ -750,9 +777,9 @@ if active_record?
|
|
750
777
|
end
|
751
778
|
end
|
752
779
|
|
753
|
-
context '
|
754
|
-
before { Bullet.
|
755
|
-
after { Bullet.
|
780
|
+
context 'add unused eager loading to safelist' do
|
781
|
+
before { Bullet.add_safelist type: :unused_eager_loading, class_name: 'Post', association: :comments }
|
782
|
+
after { Bullet.clear_safelist }
|
756
783
|
|
757
784
|
it 'should not detect unused eager loading' do
|
758
785
|
Post.includes(:comments).map(&:name)
|
@@ -28,7 +28,7 @@ if !mongoid? && active_record?
|
|
28
28
|
expect(Bullet.collected_counter_cache_notifications).to be_empty
|
29
29
|
end
|
30
30
|
|
31
|
-
if
|
31
|
+
if ActiveRecord::VERSION::MAJOR > 4
|
32
32
|
it 'should not need counter cache for has_many through' do
|
33
33
|
Client.all.each { |client| client.firms.size }
|
34
34
|
expect(Bullet.collected_counter_cache_notifications).to be_empty
|
@@ -55,9 +55,9 @@ if !mongoid? && active_record?
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
context '
|
59
|
-
before { Bullet.
|
60
|
-
after { Bullet.
|
58
|
+
context 'safelist' do
|
59
|
+
before { Bullet.add_safelist type: :counter_cache, class_name: 'Country', association: :cities }
|
60
|
+
after { Bullet.clear_safelist }
|
61
61
|
|
62
62
|
it 'should not detect counter cache' do
|
63
63
|
Country.all.each { |country| country.cities.size }
|
@@ -118,7 +118,7 @@ if mongoid?
|
|
118
118
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
119
119
|
end
|
120
120
|
|
121
|
-
it 'should detect preload with post =>
|
121
|
+
it 'should detect preload with post => comments' do
|
122
122
|
Mongoid::Post.first.comments.map(&:name)
|
123
123
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
124
124
|
expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
|
data/spec/models/deal.rb
ADDED
data/spec/models/folder.rb
CHANGED
data/spec/models/group.rb
CHANGED
data/spec/models/page.rb
CHANGED
data/spec/models/post.rb
CHANGED
@@ -4,6 +4,7 @@ class Post < ActiveRecord::Base
|
|
4
4
|
belongs_to :category, inverse_of: :posts
|
5
5
|
belongs_to :writer
|
6
6
|
has_many :comments, inverse_of: :post
|
7
|
+
has_and_belongs_to_many :deals
|
7
8
|
|
8
9
|
validates :category, presence: true
|
9
10
|
|
@@ -21,6 +22,7 @@ class Post < ActiveRecord::Base
|
|
21
22
|
next unless trigger_after_save
|
22
23
|
|
23
24
|
temp_comment = Comment.new(post: self)
|
25
|
+
|
24
26
|
# this triggers self to be "possible", even though it's
|
25
27
|
# not saved yet
|
26
28
|
temp_comment.post
|
data/spec/models/role.rb
ADDED
data/spec/models/user.rb
CHANGED
data/spec/models/writer.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
data/spec/support/mongo_seed.rb
CHANGED
@@ -45,6 +45,7 @@ module Support
|
|
45
45
|
Mongoid.configure do |config|
|
46
46
|
config.load_configuration(clients: { default: { database: 'bullet', hosts: %w[localhost:27017] } })
|
47
47
|
end
|
48
|
+
|
48
49
|
# Increase the level from DEBUG in order to avoid excessive logging to the screen
|
49
50
|
Mongo::Logger.logger.level = Logger::WARN
|
50
51
|
end
|