bullet 2.0.0.beta.2 → 2.0.0.beta.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.rspec +1 -0
  2. data/Hacking.textile +100 -0
  3. data/README.textile +36 -5
  4. data/README_for_rails2.textile +17 -0
  5. data/Rakefile +33 -16
  6. data/VERSION +1 -1
  7. data/autotest/discover.rb +1 -0
  8. data/bullet.gemspec +32 -9
  9. data/lib/bullet.rb +69 -38
  10. data/lib/bullet/action_controller2.rb +4 -4
  11. data/lib/bullet/active_record2.rb +16 -16
  12. data/lib/bullet/active_record3.rb +16 -25
  13. data/lib/bullet/detector.rb +9 -0
  14. data/lib/bullet/detector/association.rb +135 -0
  15. data/lib/bullet/detector/base.rb +19 -0
  16. data/lib/bullet/detector/counter.rb +43 -0
  17. data/lib/bullet/detector/n_plus_one_query.rb +39 -0
  18. data/lib/bullet/detector/unused_eager_association.rb +39 -0
  19. data/lib/bullet/notification.rb +4 -79
  20. data/lib/bullet/notification/base.rb +59 -0
  21. data/lib/bullet/notification/counter_cache.rb +13 -0
  22. data/lib/bullet/notification/n_plus_one_query.rb +32 -0
  23. data/lib/bullet/notification/unused_eager_loading.rb +14 -0
  24. data/lib/bullet/notification_collector.rb +25 -0
  25. data/lib/bullet/presenter.rb +13 -0
  26. data/lib/bullet/presenter/base.rb +9 -0
  27. data/lib/bullet/presenter/bullet_logger.rb +28 -0
  28. data/lib/bullet/presenter/growl.rb +40 -0
  29. data/lib/bullet/presenter/javascript_alert.rb +15 -0
  30. data/lib/bullet/presenter/javascript_console.rb +28 -0
  31. data/lib/bullet/presenter/javascript_helpers.rb +13 -0
  32. data/lib/bullet/presenter/rails_logger.rb +15 -0
  33. data/lib/bullet/presenter/xmpp.rb +56 -0
  34. data/lib/bullet/rack.rb +42 -0
  35. data/lib/bullet/registry.rb +7 -0
  36. data/lib/bullet/registry/association.rb +16 -0
  37. data/lib/bullet/registry/base.rb +39 -0
  38. data/lib/bullet/registry/object.rb +15 -0
  39. data/spec/bullet/association_for_chris_spec.rb +6 -6
  40. data/spec/bullet/association_for_peschkaj_spec.rb +6 -6
  41. data/spec/bullet/association_spec.rb +118 -262
  42. data/spec/bullet/counter_spec.rb +10 -10
  43. data/spec/spec_helper.rb +51 -17
  44. metadata +32 -9
  45. data/lib/bullet/association.rb +0 -294
  46. data/lib/bullet/counter.rb +0 -101
  47. data/lib/bullet/logger.rb +0 -9
  48. data/lib/bulletware.rb +0 -42
  49. data/spec/spec.opts +0 -3
@@ -0,0 +1,56 @@
1
+ module Bullet
2
+ module Presenter
3
+ class Xmpp < Base
4
+ @receiver = nil
5
+ @xmpp = nil
6
+ @password = nil
7
+
8
+ def self.active?
9
+ @xmpp
10
+ end
11
+
12
+ def self.out_of_channel( notice )
13
+ return unless active?
14
+ notify( notice.standard_notice )
15
+ end
16
+
17
+ def self.setup_connection( xmpp_information )
18
+ require 'xmpp4r'
19
+
20
+ @receiver = xmpp_information[:receiver]
21
+ @password = xmpp_information[:password]
22
+ @account = xmpp_information[:account]
23
+ @show_online_status = xmpp_information[:show_online_status]
24
+
25
+ connect
26
+ rescue MissingSourceFile
27
+ @xmpp = nil
28
+ raise NotificationError.new( 'You must install the xmpp4r gem to use XMPP notifications: `sudo gem install xmpp4r`' )
29
+ end
30
+
31
+ private
32
+ def self.connect
33
+ jid = Jabber::JID.new( @account )
34
+ @xmpp = Jabber::Client.new( jid )
35
+ @xmpp.connect
36
+ @xmpp.auth( @password )
37
+ @xmpp.send( presence_status ) if @show_online_status
38
+ end
39
+
40
+ def self.notify( message )
41
+ message = Jabber::Message.new( @receiver, message ).
42
+ set_type( :normal ).
43
+ set_id( '1' ).
44
+ set_subject( 'Bullet Notification' )
45
+ @xmpp.send( message )
46
+ end
47
+
48
+ def self.presence_status
49
+ project_name = Rails.root.basename.to_s.camelcase
50
+ time = Time.now
51
+
52
+ Jabber::Presence.new.set_status( "Bullet in project '#{project_name}' started on #{time}" )
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,42 @@
1
+ module Bullet
2
+ class Rack
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ return @app.call(env) unless Bullet.enable?
9
+
10
+ Bullet.start_request
11
+ status, headers, response = @app.call(env)
12
+ return [status, headers, response] if empty?(response)
13
+
14
+ if Bullet.notification?
15
+ if status == 200 and !response.body.frozen? and check_html?(headers, response)
16
+ response_body = response.body << Bullet.gather_inline_notifications
17
+ headers['Content-Length'] = response_body.length.to_s
18
+ end
19
+ Bullet.perform_out_of_channel_notifications
20
+ end
21
+ response_body ||= response.body
22
+ Bullet.end_request
23
+ no_browser_cache(headers) if Bullet.disable_browser_cache
24
+ [status, headers, [response_body]]
25
+ end
26
+
27
+ # fix issue if response's body is a Proc
28
+ def empty?(response)
29
+ (response.is_a?(Array) && response.empty?) || !response.body.is_a?(String) || response.body.empty?
30
+ end
31
+
32
+ def check_html?(headers, response)
33
+ headers['Content-Type'] and headers['Content-Type'].include? 'text/html' and response.body =~ %r{<html.*</html>}m
34
+ end
35
+
36
+ def no_browser_cache(headers)
37
+ headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
38
+ headers["Pragma"] = "no-cache"
39
+ headers["Expires"] = "Wed, 09 Sep 2009 09:09:09 GMT"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,7 @@
1
+ module Bullet
2
+ module Registry
3
+ autoload :Base, 'bullet/registry/base'
4
+ autoload :Object, 'bullet/registry/object'
5
+ autoload :Association, 'bullet/registry/association'
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ module Bullet
2
+ module Registry
3
+ class Association < Base
4
+ def merge( base, associations )
5
+ @registry.merge!( { base => associations } )
6
+ unique( @registry[base] )
7
+ end
8
+
9
+ def similarly_associated( base, associations )
10
+ @registry.select do |key, value|
11
+ key.include?( base ) and value == associations
12
+ end.collect( &:first ).flatten!
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ module Bullet
2
+ module Registry
3
+ class Base
4
+ attr_reader :registry
5
+
6
+ def initialize
7
+ @registry = {}
8
+ end
9
+
10
+ def [](key)
11
+ @registry[key]
12
+ end
13
+
14
+ def each( &block )
15
+ @registry.each( &block )
16
+ end
17
+
18
+ def delete( base )
19
+ @registry.delete( base )
20
+ end
21
+
22
+ def select( *args, &block )
23
+ @registry.select( *args, &block )
24
+ end
25
+
26
+ def add( key, value )
27
+ @registry[key] ||= []
28
+ @registry[key] << value
29
+ unique( @registry[key] )
30
+ end
31
+
32
+ private
33
+ def unique( array )
34
+ array.flatten!
35
+ array.uniq!
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ module Bullet
2
+ module Registry
3
+ class Object < Base
4
+ def add( object_or_objects )
5
+ klazz = object_or_objects.is_a?( Array ) ? object_or_objects.first.class :
6
+ object_or_objects.class
7
+ super( klazz, object_or_objects )
8
+ end
9
+
10
+ def contains?( object )
11
+ @registry[object.class] and @registry[object.class].include?( object )
12
+ end
13
+ end
14
+ end
15
+ end
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
4
4
  # This test is just used for http://github.com/flyerhzm/bullet/issues/#issue/14
5
- describe Bullet::Association do
5
+ describe Bullet::Detector::Association do
6
6
 
7
7
  describe "for chris" do
8
8
  def setup_db
@@ -65,32 +65,32 @@ describe Bullet::Association do
65
65
  end
66
66
 
67
67
  before(:each) do
68
- Bullet::Association.start_request
68
+ Bullet.start_request
69
69
  end
70
70
 
71
71
  after(:each) do
72
- Bullet::Association.end_request
72
+ Bullet.end_request
73
73
  end
74
74
 
75
75
  it "should detect unpreload association from deal to hotel" do
76
76
  Deal.all.each do |deal|
77
77
  deal.hotel.location.name
78
78
  end
79
- Bullet::Association.should be_detecting_unpreloaded_association_for(Deal, :hotel)
79
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Deal, :hotel)
80
80
  end
81
81
 
82
82
  it "should detect unpreload association from hotel to location" do
83
83
  Deal.includes(:hotel).each do |deal|
84
84
  deal.hotel.location.name
85
85
  end
86
- Bullet::Association.should be_detecting_unpreloaded_association_for(Hotel, :location)
86
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Hotel, :location)
87
87
  end
88
88
 
89
89
  it "should not detect unpreload association" do
90
90
  Deal.includes({:hotel => :location}).each do |deal|
91
91
  deal.hotel.location.name
92
92
  end
93
- Bullet::Association.should_not be_has_unused_preload_associations
93
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
94
94
  end
95
95
  end
96
96
  end
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
4
4
  # This test is just used for http://github.com/flyerhzm/bullet/issues#issue/20
5
- describe Bullet::Association do
5
+ describe Bullet::Detector::Association do
6
6
 
7
7
  describe "for peschkaj" do
8
8
  def setup_db
@@ -65,11 +65,11 @@ describe Bullet::Association do
65
65
  end
66
66
 
67
67
  before(:each) do
68
- Bullet::Association.start_request
68
+ Bullet.start_request
69
69
  end
70
70
 
71
71
  after(:each) do
72
- Bullet::Association.end_request
72
+ Bullet.end_request
73
73
  end
74
74
 
75
75
  it "should not detect unused preload associations" do
@@ -78,9 +78,9 @@ describe Bullet::Association do
78
78
  submission.name
79
79
  submission.user.name
80
80
  end
81
- Bullet::Association.check_unused_preload_associations
82
- Bullet::Association.should_not be_unused_preload_associations_for(Category, :submissions)
83
- Bullet::Association.should_not be_unused_preload_associations_for(Submission, :user)
81
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
82
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Category, :submissions)
83
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Submission, :user)
84
84
  end
85
85
  end
86
86
  end
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
4
4
 
5
- describe Bullet::Association, 'has_many' do
5
+ describe Bullet::Detector::Association, 'has_many' do
6
6
 
7
7
  def setup_db
8
8
  ActiveRecord::Schema.define(:version => 1) do
@@ -121,11 +121,11 @@ describe Bullet::Association, 'has_many' do
121
121
  end
122
122
 
123
123
  before(:each) do
124
- Bullet::Association.start_request
124
+ Bullet.start_request
125
125
  end
126
126
 
127
127
  after(:each) do
128
- Bullet::Association.end_request
128
+ Bullet.end_request
129
129
  end
130
130
 
131
131
  # FIXME: setup and teardown are not inherited by context
@@ -159,8 +159,8 @@ describe Bullet::Association, 'has_many' do
159
159
  comments_with_author = Comment.includes(:author)
160
160
  comment_collection = comments_with_author.limit(2)
161
161
  comment_collection.collect { |com| com.author.name }
162
- Bullet::Association.check_unused_preload_associations
163
- Bullet::Association.should_not be_unused_preload_associations_for(Comment, :author)
162
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
163
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Comment, :author)
164
164
  end
165
165
  # end
166
166
 
@@ -174,7 +174,7 @@ describe Bullet::Association, 'has_many' do
174
174
  Comment.includes([:author, :post]).where(["base_users.id = ?", BaseUser.first]).each do |com|
175
175
  com.post.writer.name
176
176
  end
177
- Bullet::Association.should be_detecting_unpreloaded_association_for(Post, :writer)
177
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :writer)
178
178
  end
179
179
 
180
180
  # this happens because the comment doesn't break down the hash into keys
@@ -183,17 +183,17 @@ describe Bullet::Association, 'has_many' do
183
183
  comments = Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).each do |com|
184
184
  com.post.writer.name
185
185
  end
186
- Bullet::Association.should_not be_detecting_unpreloaded_association_for(Comment, :post)
187
- Bullet::Association.should be_completely_preloading_associations
186
+ Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Comment, :post)
187
+ Bullet::Detector::Association.should be_completely_preloading_associations
188
188
  end
189
189
 
190
190
  it "should detect preload of post => writer" do
191
191
  comments = Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).each do |com|
192
192
  com.post.writer.name
193
193
  end
194
- Bullet::Association.should be_creating_object_association_for(comments.first, :author)
195
- Bullet::Association.should_not be_detecting_unpreloaded_association_for(Post, :writer)
196
- Bullet::Association.should be_completely_preloading_associations
194
+ Bullet::Detector::Association.should be_creating_object_association_for(comments.first, :author)
195
+ Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Post, :writer)
196
+ Bullet::Detector::Association.should be_completely_preloading_associations
197
197
  end
198
198
 
199
199
  # To flyerhzm: This does not detect that newspaper is unpreloaded. The association is
@@ -202,7 +202,7 @@ describe Bullet::Association, 'has_many' do
202
202
  comments = Comment.all(:include => {:post => :writer}, :conditions => "posts.name like '%first%'").each do |com|
203
203
  com.post.writer.newspaper.name
204
204
  end
205
- Bullet::Association.should be_detecting_unpreloaded_association_for(Writer, :newspaper)
205
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Writer, :newspaper)
206
206
  end
207
207
 
208
208
  # when we attempt to access category, there is an infinite overflow because load_target is hijacked leading to
@@ -218,47 +218,51 @@ describe Bullet::Association, 'has_many' do
218
218
 
219
219
  # FIXME: setup and teardown are not inherited by context
220
220
  # context "post => comments" do
221
+ #
222
+ ### FIXME: Please double check semantic equivalence with original
221
223
  it "should detect preload with post => comments" do
222
224
  Post.includes(:comments).each do |post|
223
225
  post.comments.collect(&:name)
224
226
  end
225
- Bullet::Association.should_not be_has_unpreload_associations
227
+ # Bullet::Detector::Association.should_not be_has_unpreload_associations
228
+ Bullet::Detector::Association.should be_completely_preloading_associations
226
229
  end
227
230
 
228
231
  it "should detect no preload post => comments" do
229
232
  Post.all.each do |post|
230
233
  post.comments.collect(&:name)
231
234
  end
232
- Bullet::Association.should be_has_unpreload_associations
235
+ # Bullet::Detector::Association.should be_has_unpreload_associations
236
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
233
237
  end
234
238
 
235
239
  it "should detect unused preload post => comments for post" do
236
240
  Post.includes(:comments).collect(&:name)
237
- Bullet::Association.check_unused_preload_associations
238
- Bullet::Association.should be_has_unused_preload_associations
241
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
242
+ Bullet::Detector::Association.should be_has_unused_preload_associations
239
243
  end
240
244
 
241
245
  it "should detect no unused preload post => comments for post" do
242
246
  Post.all.collect(&:name)
243
- Bullet::Association.check_unused_preload_associations
244
- Bullet::Association.should_not be_has_unused_preload_associations
247
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
248
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
245
249
  end
246
250
 
247
251
  it "should detect no unused preload post => comments for comment" do
248
252
  Post.all.each do |post|
249
253
  post.comments.collect(&:name)
250
254
  end
251
- Bullet::Association.check_unused_preload_associations
252
- Bullet::Association.should_not be_has_unused_preload_associations
255
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
256
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
253
257
 
254
- Bullet::Association.end_request
255
- Bullet::Association.start_request
258
+ Bullet.end_request
259
+ Bullet.start_request
256
260
 
257
261
  Post.all.each do |post|
258
262
  post.comments.collect(&:name)
259
263
  end
260
- Bullet::Association.check_unused_preload_associations
261
- Bullet::Association.should_not be_has_unused_preload_associations
264
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
265
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
262
266
  end
263
267
  # end
264
268
 
@@ -270,7 +274,8 @@ describe Bullet::Association, 'has_many' do
270
274
  post.comments.collect(&:name)
271
275
  end
272
276
  end
273
- Bullet::Association.should_not be_has_unpreload_associations
277
+ # Bullet::Detector::Association.should_not be_has_unpreload_associations
278
+ Bullet::Detector::Association.should be_completely_preloading_associations
274
279
  end
275
280
 
276
281
  it "should detect preload category => posts, but no post => comments" do
@@ -279,7 +284,8 @@ describe Bullet::Association, 'has_many' do
279
284
  post.comments.collect(&:name)
280
285
  end
281
286
  end
282
- Bullet::Association.should be_has_unpreload_associations
287
+ # Bullet::Detector::Association.should be_has_unpreload_associations
288
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
283
289
  end
284
290
 
285
291
  it "should detect no preload category => posts => comments" do
@@ -288,21 +294,22 @@ describe Bullet::Association, 'has_many' do
288
294
  post.comments.collect(&:name)
289
295
  end
290
296
  end
291
- Bullet::Association.should be_has_unpreload_associations
297
+ # Bullet::Detector::Association.should be_has_unpreload_associations
298
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
292
299
  end
293
300
 
294
301
  it "should detect unused preload with category => posts => comments" do
295
302
  Category.includes({:posts => :comments}).collect(&:name)
296
- Bullet::Association.check_unused_preload_associations
297
- Bullet::Association.should be_has_unused_preload_associations
303
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
304
+ Bullet::Detector::Association.should be_has_unused_preload_associations
298
305
  end
299
306
 
300
307
  it "should detect unused preload with post => commnets, no category => posts" do
301
308
  Category.includes({:posts => :comments}).each do |category|
302
309
  category.posts.collect(&:name)
303
310
  end
304
- Bullet::Association.check_unused_preload_associations
305
- Bullet::Association.should be_has_unused_preload_associations
311
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
312
+ Bullet::Detector::Association.should be_has_unused_preload_associations
306
313
  end
307
314
 
308
315
  it "should no detect preload with category => posts => comments" do
@@ -311,8 +318,8 @@ describe Bullet::Association, 'has_many' do
311
318
  post.comments.collect(&:name)
312
319
  end
313
320
  end
314
- Bullet::Association.check_unused_preload_associations
315
- Bullet::Association.should_not be_has_unused_preload_associations
321
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
322
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
316
323
  end
317
324
  # end
318
325
 
@@ -323,7 +330,7 @@ describe Bullet::Association, 'has_many' do
323
330
  category.posts.collect(&:name)
324
331
  category.entries.collect(&:name)
325
332
  end
326
- Bullet::Association.should_not be_has_unpreload_associations
333
+ Bullet::Detector::Association.should be_completely_preloading_associations
327
334
  end
328
335
 
329
336
  it "should detect preload with category => posts, but no category => entries" do
@@ -331,7 +338,7 @@ describe Bullet::Association, 'has_many' do
331
338
  category.posts.collect(&:name)
332
339
  category.entries.collect(&:name)
333
340
  end
334
- Bullet::Association.should be_has_unpreload_associations
341
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
335
342
  end
336
343
 
337
344
  it "should detect no preload with category => [posts, entries]" do
@@ -339,21 +346,21 @@ describe Bullet::Association, 'has_many' do
339
346
  category.posts.collect(&:name)
340
347
  category.entries.collect(&:name)
341
348
  end
342
- Bullet::Association.should be_has_unpreload_associations
349
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
343
350
  end
344
351
 
345
352
  it "should detect unused with category => [posts, entries]" do
346
353
  Category.includes([:posts, :entries]).collect(&:name)
347
- Bullet::Association.check_unused_preload_associations
348
- Bullet::Association.should be_has_unused_preload_associations
354
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
355
+ Bullet::Detector::Association.should be_has_unused_preload_associations
349
356
  end
350
357
 
351
358
  it "should detect unused preload with category => entries, but no category => posts" do
352
359
  Category.includes([:posts, :entries]).each do |category|
353
360
  category.posts.collect(&:name)
354
361
  end
355
- Bullet::Association.check_unused_preload_associations
356
- Bullet::Association.should be_has_unused_preload_associations
362
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
363
+ Bullet::Detector::Association.should be_has_unused_preload_associations
357
364
  end
358
365
 
359
366
  it "should detect no unused preload" do
@@ -361,8 +368,8 @@ describe Bullet::Association, 'has_many' do
361
368
  category.posts.collect(&:name)
362
369
  category.entries.collect(&:name)
363
370
  end
364
- Bullet::Association.check_unused_preload_associations
365
- Bullet::Association.should_not be_has_unused_preload_associations
371
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
372
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
366
373
  end
367
374
  # end
368
375
 
@@ -372,12 +379,12 @@ describe Bullet::Association, 'has_many' do
372
379
  Post.includes(:comments).each do |post|
373
380
  post.comments.first.name
374
381
  end
375
- Bullet::Association.should_not be_has_unpreload_associations
382
+ Bullet::Detector::Association.should be_completely_preloading_associations
376
383
  end
377
384
 
378
385
  it "should no preload only one post => commnets" do
379
386
  Post.first.comments.collect(&:name)
380
- Bullet::Association.should_not be_has_unpreload_associations
387
+ Bullet::Detector::Association.should be_completely_preloading_associations
381
388
  end
382
389
  # end
383
390
 
@@ -387,14 +394,14 @@ describe Bullet::Association, 'has_many' do
387
394
  Post.in_category_name('first').all.each do |post|
388
395
  post.category.name
389
396
  end
390
- Bullet::Association.should_not be_has_unpreload_associations
397
+ Bullet::Detector::Association.should be_completely_preloading_associations
391
398
  end
392
399
 
393
400
  it "should not be unused preload post => category" do
394
401
  Post.in_category_name('first').all.collect(&:name)
395
- Bullet::Association.should_not be_has_unpreload_associations
396
- Bullet::Association.check_unused_preload_associations
397
- Bullet::Association.should_not be_has_unused_preload_associations
402
+ Bullet::Detector::Association.should be_completely_preloading_associations
403
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
404
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
398
405
  end
399
406
  # end
400
407
 
@@ -404,14 +411,14 @@ describe Bullet::Association, 'has_many' do
404
411
  Post.preload_posts.each do |post|
405
412
  post.comments.collect(&:name)
406
413
  end
407
- Bullet::Association.should_not be_has_unpreload_associations
414
+ Bullet::Detector::Association.should be_completely_preloading_associations
408
415
  end
409
416
 
410
417
  it "should unused preload with scope" do
411
418
  Post.preload_posts.collect(&:name)
412
- Bullet::Association.should_not be_has_unpreload_associations
413
- Bullet::Association.check_unused_preload_associations
414
- Bullet::Association.should be_has_unused_preload_associations
419
+ Bullet::Detector::Association.should be_completely_preloading_associations
420
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
421
+ Bullet::Detector::Association.should be_has_unused_preload_associations
415
422
  end
416
423
  # end
417
424
 
@@ -428,8 +435,8 @@ describe Bullet::Association, 'has_many' do
428
435
  end
429
436
  end
430
437
  end
431
- Bullet::Association.check_unused_preload_associations
432
- Bullet::Association.should_not be_has_unused_preload_associations
438
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
439
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
433
440
  end
434
441
  # end
435
442
 
@@ -439,52 +446,52 @@ describe Bullet::Association, 'has_many' do
439
446
  Comment.all.each do |comment|
440
447
  comment.post.name
441
448
  end
442
- Bullet::Association.should be_has_unpreload_associations
449
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
443
450
  end
444
451
 
445
452
  it "should no preload comment => post" do
446
453
  Comment.first.post.name
447
- Bullet::Association.should_not be_has_unpreload_associations
454
+ Bullet::Detector::Association.should be_completely_preloading_associations
448
455
  end
449
456
 
450
457
  it "should no preload comments => post" do
451
458
  Comment.includes(:post).each do |comment|
452
459
  comment.post.name
453
460
  end
454
- Bullet::Association.should_not be_has_unpreload_associations
461
+ Bullet::Detector::Association.should be_completely_preloading_associations
455
462
  end
456
463
 
457
464
  it "should detect no unused preload comments => post" do
458
465
  Comment.all.collect(&:name)
459
- Bullet::Association.check_unused_preload_associations
460
- Bullet::Association.should_not be_has_unused_preload_associations
466
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
467
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
461
468
  end
462
469
 
463
470
  it "should detect unused preload comments => post" do
464
471
  Comment.includes(:post).collect(&:name)
465
- Bullet::Association.check_unused_preload_associations
466
- Bullet::Association.should be_has_unused_preload_associations
472
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
473
+ Bullet::Detector::Association.should be_has_unused_preload_associations
467
474
  end
468
475
 
469
476
  it "should dectect no unused preload comments => post" do
470
477
  Comment.all.each do |comment|
471
478
  comment.post.name
472
479
  end
473
- Bullet::Association.check_unused_preload_associations
474
- Bullet::Association.should_not be_has_unused_preload_associations
480
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
481
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
475
482
  end
476
483
 
477
484
  it "should dectect no unused preload comments => post" do
478
485
  Comment.includes(:post).each do |comment|
479
486
  comment.post.name
480
487
  end
481
- Bullet::Association.check_unused_preload_associations
482
- Bullet::Association.should_not be_has_unused_preload_associations
488
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
489
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
483
490
  end
484
491
  # end
485
492
  end
486
493
 
487
- describe Bullet::Association, 'has_and_belongs_to_many' do
494
+ describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
488
495
 
489
496
  def setup_db
490
497
  ActiveRecord::Schema.define(:version => 1) do
@@ -534,41 +541,41 @@ describe Bullet::Association, 'has_and_belongs_to_many' do
534
541
  end
535
542
 
536
543
  before(:each) do
537
- Bullet::Association.start_request
544
+ Bullet.start_request
538
545
  end
539
546
 
540
547
  after(:each) do
541
- Bullet::Association.end_request
548
+ Bullet.end_request
542
549
  end
543
550
 
544
551
  it "should detect unpreload associations" do
545
552
  Student.all.each do |student|
546
553
  student.teachers.collect(&:name)
547
554
  end
548
- Bullet::Association.should be_has_unpreload_associations
555
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
549
556
  end
550
557
 
551
558
  it "should detect no unpreload associations" do
552
559
  Student.includes(:teachers).each do |student|
553
560
  student.teachers.collect(&:name)
554
561
  end
555
- Bullet::Association.should_not be_has_unpreload_associations
562
+ Bullet::Detector::Association.should be_completely_preloading_associations
556
563
  end
557
564
 
558
565
  it "should detect unused preload associations" do
559
566
  Student.includes(:teachers).collect(&:name)
560
- Bullet::Association.check_unused_preload_associations
561
- Bullet::Association.should be_has_unused_preload_associations
567
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
568
+ Bullet::Detector::Association.should be_has_unused_preload_associations
562
569
  end
563
570
 
564
571
  it "should detect no unused preload associations" do
565
572
  Student.all.collect(&:name)
566
- Bullet::Association.check_unused_preload_associations
567
- Bullet::Association.should_not be_has_unused_preload_associations
573
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
574
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
568
575
  end
569
576
  end
570
577
 
571
- describe Bullet::Association, 'has_many :through' do
578
+ describe Bullet::Detector::Association, 'has_many :through' do
572
579
 
573
580
  def setup_db
574
581
  ActiveRecord::Schema.define(:version => 1) do
@@ -625,194 +632,43 @@ describe Bullet::Association, 'has_many :through' do
625
632
  end
626
633
 
627
634
  before(:each) do
628
- Bullet::Association.start_request
635
+ Bullet.start_request
629
636
  end
630
637
 
631
638
  after(:each) do
632
- Bullet::Association.end_request
639
+ Bullet.end_request
633
640
  end
634
641
 
635
642
  it "should detect unpreload associations" do
636
643
  Firm.all.each do |firm|
637
644
  firm.clients.collect(&:name)
638
645
  end
639
- Bullet::Association.should be_has_unpreload_associations
646
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
640
647
  end
641
648
 
642
649
  it "should detect no unpreload associations" do
643
650
  Firm.includes(:clients).each do |firm|
644
651
  firm.clients.collect(&:name)
645
652
  end
646
- Bullet::Association.should_not be_has_unpreload_associations
653
+ Bullet::Detector::Association.should be_completely_preloading_associations
647
654
  end
648
655
 
649
656
  it "should detect no unused preload associations" do
650
657
  Firm.all.collect(&:name)
651
- Bullet::Association.check_unused_preload_associations
652
- Bullet::Association.should_not be_has_unused_preload_associations
658
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
659
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
653
660
  end
654
661
 
655
662
  it "should detect unused preload associations" do
656
663
  Firm.includes(:clients).collect(&:name)
657
- Bullet::Association.check_unused_preload_associations
658
- Bullet::Association.should be_has_unused_preload_associations
664
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
665
+ Bullet::Detector::Association.should be_has_unused_preload_associations
659
666
  end
660
667
  end
661
668
 
662
- describe Bullet::Association, 'has_many :as' do
663
669
 
664
- def setup_db
665
- ActiveRecord::Schema.define(:version => 1) do
666
- create_table :votes do |t|
667
- t.column :vote, :integer
668
- t.references :voteable, :polymorphic => true
669
- end
670
-
671
- create_table :users do |t|
672
- t.column :name, :string
673
- end
674
-
675
- create_table :pets do |t|
676
- t.column :name, :string
677
- t.column :user_id, :integer
678
- end
679
-
680
- create_table :news do |t|
681
- t.column :name, :string
682
- end
683
- end
684
- end
685
-
686
- def teardown_db
687
- ActiveRecord::Base.connection.tables.each do |table|
688
- ActiveRecord::Base.connection.drop_table(table)
689
- end
690
- end
691
-
692
- class Vote < ActiveRecord::Base
693
- belongs_to :voteable, :polymorphic => true
694
- end
695
-
696
- class User < ActiveRecord::Base
697
- has_many :votes, :as => :voteable
698
- has_many :pets
699
- end
700
-
701
- class Pet < ActiveRecord::Base
702
- belongs_to :user
703
- end
704
-
705
- class News < ActiveRecord::Base
706
- has_many :votes, :as => :voteable
707
- end
708
-
709
- before(:all) do
710
- setup_db
711
- user1 = User.create(:name => 'first')
712
- user2 = User.create(:name => 'second')
713
- user3 = User.create(:name => 'third')
714
- user4 = User.create(:name => 'fourth')
715
-
716
- pet1 = user1.pets.create(:name => "dog")
717
- pet2 = user1.pets.create(:name => "dog")
718
- pet3 = user2.pets.create(:name => "cat")
719
- pet4 = user2.pets.create(:name => "cat")
720
-
721
- user1.votes.create(:vote => 10)
722
- user1.votes.create(:vote => 20)
723
- user2.votes.create(:vote => 10)
724
- user2.votes.create(:vote => 20)
725
- user3.votes.create(:vote => 10)
726
- user3.votes.create(:vote => 20)
727
- user4.votes.create(:vote => 10)
728
- user4.votes.create(:vote => 20)
729
-
730
- news1 = News.create(:name => 'first')
731
- news2 = News.create(:name => 'second')
732
- news1.votes.create(:vote => 10)
733
- news1.votes.create(:vote => 20)
734
- news2.votes.create(:vote => 10)
735
- news2.votes.create(:vote => 20)
736
- end
737
-
738
- after(:all) do
739
- teardown_db
740
- end
741
-
742
- before(:each) do
743
- Bullet::Association.start_request
744
- end
745
-
746
- after(:each) do
747
- Bullet::Association.end_request
748
- end
749
-
750
- # this happens only when a polymorphic association is included along with another table which is being referenced in the query
751
- it "should not have unused preloaded associations with conditions" do
752
- all_users = User.includes(:pets)
753
- users_with_ten_votes = User.includes(:votes).where(["votes.vote = ?", 10])
754
- users_without_ten_votes = User.where(["users.id not in (?)", users_with_ten_votes.collect(&:id)])
755
- puts all_users.to_sql
756
- all_users.each { |t| t.pets.collect(&:name) }
757
- Bullet::Association.check_unused_preload_associations
758
- Bullet::Association.should_not be_unused_preload_associations_for(User, :pets)
759
- Bullet::Association.should_not be_unused_preload_associations_for(User, :votes)
760
- end
761
-
762
- it "should detect unpreload associations" do
763
- User.all.each do |user|
764
- user.votes.collect(&:vote)
765
- end
766
- Bullet::Association.should be_has_unpreload_associations
767
- end
768
-
769
- it "should detect no unpreload associations" do
770
- User.includes(:votes).each do |user|
771
- user.votes.collect(&:vote)
772
- end
773
- Bullet::Association.should_not be_has_unpreload_associations
774
- end
775
-
776
- it "should detect unpreload associations with voteable" do
777
- Vote.all.each do |vote|
778
- vote.voteable.name
779
- end
780
- Bullet::Association.should be_has_unpreload_associations
781
- end
782
-
783
- it "should detect no unpreload associations with voteable" do
784
- Vote.includes(:voteable).each do |vote|
785
- vote.voteable.name
786
- end
787
- Bullet::Association.should_not be_has_unpreload_associations
788
- end
789
-
790
- it "should detect no unused preload associations" do
791
- User.all.collect(&:name)
792
- Bullet::Association.check_unused_preload_associations
793
- Bullet::Association.should_not be_has_unused_preload_associations
794
- end
795
-
796
- it "should detect unused preload associations" do
797
- User.includes(:votes).collect(&:name)
798
- Bullet::Association.check_unused_preload_associations
799
- Bullet::Association.should be_has_unused_preload_associations
800
- end
801
-
802
- it "should detect no unused preload associations with voteable" do
803
- Vote.all.collect(&:vote)
804
- Bullet::Association.check_unused_preload_associations
805
- Bullet::Association.should_not be_has_unused_preload_associations
806
- end
807
-
808
- it "should detect unused preload associations with voteable" do
809
- Vote.includes(:voteable).collect(&:vote)
810
- Bullet::Association.check_unused_preload_associations
811
- Bullet::Association.should be_has_unused_preload_associations
812
- end
813
- end
814
670
 
815
- describe Bullet::Association, "has_one" do
671
+ describe Bullet::Detector::Association, "has_one" do
816
672
 
817
673
  def setup_db
818
674
  ActiveRecord::Schema.define(:version => 1) do
@@ -856,41 +712,41 @@ describe Bullet::Association, "has_one" do
856
712
  end
857
713
 
858
714
  before(:each) do
859
- Bullet::Association.start_request
715
+ Bullet.start_request
860
716
  end
861
717
 
862
718
  after(:each) do
863
- Bullet::Association.end_request
719
+ Bullet.end_request
864
720
  end
865
721
 
866
722
  it "should detect unpreload association" do
867
723
  Company.all.each do |company|
868
724
  company.address.name
869
725
  end
870
- Bullet::Association.should be_has_unpreload_associations
726
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
871
727
  end
872
728
 
873
729
  it "should detect no unpreload association" do
874
730
  Company.find(:all, :include => :address).each do |company|
875
731
  company.address.name
876
732
  end
877
- Bullet::Association.should_not be_has_unpreload_associations
733
+ Bullet::Detector::Association.should be_completely_preloading_associations
878
734
  end
879
735
 
880
736
  it "should detect no unused preload association" do
881
737
  Company.all.collect(&:name)
882
- Bullet::Association.check_unused_preload_associations
883
- Bullet::Association.should_not be_has_unused_preload_associations
738
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
739
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
884
740
  end
885
741
 
886
742
  it "should detect unused preload association" do
887
743
  Company.find(:all, :include => :address).collect(&:name)
888
- Bullet::Association.check_unused_preload_associations
889
- Bullet::Association.should be_has_unused_preload_associations
744
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
745
+ Bullet::Detector::Association.should be_has_unused_preload_associations
890
746
  end
891
747
  end
892
748
 
893
- describe Bullet::Association, "call one association that in possible objects" do
749
+ describe Bullet::Detector::Association, "call one association that in possible objects" do
894
750
 
895
751
  def setup_db
896
752
  ActiveRecord::Schema.define(:version => 1) do
@@ -936,21 +792,21 @@ describe Bullet::Association, "call one association that in possible objects" do
936
792
  end
937
793
 
938
794
  before(:each) do
939
- Bullet::Association.start_request
795
+ Bullet.start_request
940
796
  end
941
797
 
942
798
  after(:each) do
943
- Bullet::Association.end_request
799
+ Bullet.end_request
944
800
  end
945
801
 
946
802
  it "should detect no unpreload association" do
947
803
  Contact.all
948
804
  Contact.first.emails.collect(&:name)
949
- Bullet::Association.should_not be_has_unpreload_associations
805
+ Bullet::Detector::Association.should be_completely_preloading_associations
950
806
  end
951
807
  end
952
808
 
953
- describe Bullet::Association, "STI" do
809
+ describe Bullet::Detector::Association, "STI" do
954
810
 
955
811
  def setup_db
956
812
  ActiveRecord::Schema.define(:version => 1) do
@@ -1002,42 +858,42 @@ describe Bullet::Association, "STI" do
1002
858
  end
1003
859
 
1004
860
  before(:each) do
1005
- Bullet::Association.start_request
861
+ Bullet.start_request
1006
862
  end
1007
863
 
1008
864
  after(:each) do
1009
- Bullet::Association.end_request
865
+ Bullet.end_request
1010
866
  end
1011
867
 
1012
868
  it "should detect unpreload associations" do
1013
869
  Page.all.each do |page|
1014
870
  page.author.name
1015
871
  end
1016
- Bullet::Association.should be_has_unpreload_associations
1017
- Bullet::Association.check_unused_preload_associations
1018
- Bullet::Association.should_not be_has_unused_preload_associations
872
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
873
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
874
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
1019
875
  end
1020
876
 
1021
877
  it "should not detect unpreload associations" do
1022
878
  Page.find(:all, :include => :author).each do |page|
1023
879
  page.author.name
1024
880
  end
1025
- Bullet::Association.should_not be_has_unpreload_associations
1026
- Bullet::Association.check_unused_preload_associations
1027
- Bullet::Association.should_not be_has_unused_preload_associations
881
+ Bullet::Detector::Association.should be_completely_preloading_associations
882
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
883
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
1028
884
  end
1029
885
 
1030
886
  it "should detect unused preload associations" do
1031
887
  Page.find(:all, :include => :author).collect(&:name)
1032
- Bullet::Association.should_not be_has_unpreload_associations
1033
- Bullet::Association.check_unused_preload_associations
1034
- Bullet::Association.should be_has_unused_preload_associations
888
+ Bullet::Detector::Association.should be_completely_preloading_associations
889
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
890
+ Bullet::Detector::Association.should be_has_unused_preload_associations
1035
891
  end
1036
892
 
1037
893
  it "should not detect unused preload associations" do
1038
894
  Page.all.collect(&:name)
1039
- Bullet::Association.should_not be_has_unpreload_associations
1040
- Bullet::Association.check_unused_preload_associations
1041
- Bullet::Association.should_not be_has_unused_preload_associations
895
+ Bullet::Detector::Association.should be_completely_preloading_associations
896
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
897
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
1042
898
  end
1043
899
  end