bullet 4.3.1 → 4.5.0
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/.travis.yml +4 -4
- data/CHANGELOG.md +31 -0
- data/{Hacking.textile → Hacking.md} +10 -5
- data/{README.textile → README.md} +174 -144
- data/lib/bullet/active_record2.rb +9 -9
- data/lib/bullet/active_record3.rb +9 -9
- data/lib/bullet/active_record3x.rb +11 -11
- data/lib/bullet/active_record4.rb +94 -0
- data/lib/bullet/dependency.rb +3 -1
- data/lib/bullet/detector/association.rb +2 -43
- data/lib/bullet/detector/{counter.rb → counter_cache.rb} +13 -3
- data/lib/bullet/detector/n_plus_one_query.rb +21 -2
- data/lib/bullet/detector/{unused_eager_association.rb → unused_eager_loading.rb} +40 -3
- data/lib/bullet/detector.rb +2 -2
- data/lib/bullet/mongoid2x.rb +5 -5
- data/lib/bullet/mongoid3x.rb +6 -6
- data/lib/bullet/version.rb +1 -1
- data/lib/bullet.rb +42 -7
- data/spec/bullet/detector/association_spec.rb +0 -49
- data/spec/bullet/detector/counter_cache_spec.rb +64 -0
- data/spec/bullet/detector/n_plus_one_query_spec.rb +23 -1
- data/spec/bullet/detector/unused_eager_loading_spec.rb +95 -0
- data/spec/integration/association_spec.rb +155 -63
- data/spec/integration/{counter_spec.rb → counter_cache_spec.rb} +26 -2
- data/spec/integration/mongoid/association_spec.rb +24 -24
- data/spec/integration/rails2/association_spec.rb +49 -49
- data/spec/integration/rails2/{counter_spec.rb → counter_cache_spec.rb} +13 -1
- metadata +17 -15
- data/spec/bullet/detector/counter_spec.rb +0 -64
- data/spec/bullet/detector/unused_eager_association_spec.rb +0 -62
|
@@ -40,55 +40,6 @@ module Bullet
|
|
|
40
40
|
Association.send(:call_object_associations).should be_include(@post1.bullet_ar_key, :associations)
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
|
-
|
|
44
|
-
context ".add_possible_objects" do
|
|
45
|
-
it "should add possible objects" do
|
|
46
|
-
Association.add_possible_objects([@post1, @post2])
|
|
47
|
-
Association.send(:possible_objects).should be_include(@post1.bullet_ar_key)
|
|
48
|
-
Association.send(:possible_objects).should be_include(@post2.bullet_ar_key)
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
it "should not raise error if object is nil" do
|
|
52
|
-
lambda { Association.add_possible_objects(nil) }.should_not raise_error
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
context ".add_impossible_object" do
|
|
57
|
-
it "should add impossible object" do
|
|
58
|
-
Association.add_impossible_object(@post1)
|
|
59
|
-
Association.send(:impossible_objects).should be_include(@post1.bullet_ar_key)
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
context ".add_eager_loadings" do
|
|
64
|
-
it "should add objects, associations pair when eager_loadings are empty" do
|
|
65
|
-
Association.add_eager_loadings([@post1, @post2], :associations)
|
|
66
|
-
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key, @post2.bullet_ar_key], :associations)
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
it "should add objects, associations pair for existing eager_loadings" do
|
|
70
|
-
Association.add_eager_loadings([@post1, @post2], :association1)
|
|
71
|
-
Association.add_eager_loadings([@post1, @post2], :association2)
|
|
72
|
-
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key, @post2.bullet_ar_key], :association1)
|
|
73
|
-
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key, @post2.bullet_ar_key], :association2)
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
it "should merge objects, associations pair for existing eager_loadings" do
|
|
77
|
-
Association.add_eager_loadings([@post1], :association1)
|
|
78
|
-
Association.add_eager_loadings([@post1, @post2], :association2)
|
|
79
|
-
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key], :association1)
|
|
80
|
-
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key], :association2)
|
|
81
|
-
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key, @post2.bullet_ar_key], :association2)
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
it "should delete objects, associations pair for existing eager_loadings" do
|
|
85
|
-
Association.add_eager_loadings([@post1, @post2], :association1)
|
|
86
|
-
Association.add_eager_loadings([@post1], :association2)
|
|
87
|
-
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key], :association1)
|
|
88
|
-
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key], :association2)
|
|
89
|
-
Association.send(:eager_loadings).should be_include([@post2.bullet_ar_key], :association1)
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
43
|
end
|
|
93
44
|
end
|
|
94
45
|
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
module Bullet
|
|
4
|
+
module Detector
|
|
5
|
+
describe CounterCache do
|
|
6
|
+
before :all do
|
|
7
|
+
@post1 = Post.first
|
|
8
|
+
@post2 = Post.last
|
|
9
|
+
end
|
|
10
|
+
before(:each) { CounterCache.clear }
|
|
11
|
+
|
|
12
|
+
context ".clear" do
|
|
13
|
+
it "should clear all class variables" do
|
|
14
|
+
CounterCache.class_variable_get(:@@possible_objects).should be_nil
|
|
15
|
+
CounterCache.class_variable_get(:@@impossible_objects).should be_nil
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
context ".add_counter_cache" do
|
|
20
|
+
it "should create notification if conditions met" do
|
|
21
|
+
CounterCache.should_receive(:conditions_met?).with(@post1.bullet_ar_key, [:comments]).and_return(true)
|
|
22
|
+
CounterCache.should_receive(:create_notification).with("Post", [:comments])
|
|
23
|
+
CounterCache.add_counter_cache(@post1, [:comments])
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "should not create notification if conditions not met" do
|
|
27
|
+
CounterCache.should_receive(:conditions_met?).with(@post1.bullet_ar_key, [:comments]).and_return(false)
|
|
28
|
+
CounterCache.should_receive(:create_notification).never
|
|
29
|
+
CounterCache.add_counter_cache(@post1, [:comments])
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
context ".add_possible_objects" do
|
|
34
|
+
it "should add possible objects" do
|
|
35
|
+
CounterCache.add_possible_objects([@post1, @post2])
|
|
36
|
+
CounterCache.send(:possible_objects).should be_include(@post1.bullet_ar_key)
|
|
37
|
+
CounterCache.send(:possible_objects).should be_include(@post2.bullet_ar_key)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "should add impossible object" do
|
|
41
|
+
CounterCache.add_impossible_object(@post1)
|
|
42
|
+
CounterCache.send(:impossible_objects).should be_include(@post1.bullet_ar_key)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context ".conditions_met?" do
|
|
47
|
+
it "should be true when object is possible, not impossible" do
|
|
48
|
+
CounterCache.add_possible_objects(@post1)
|
|
49
|
+
CounterCache.send(:conditions_met?, @post1.bullet_ar_key, :associations).should be_true
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "should be false when object is not possible" do
|
|
53
|
+
CounterCache.send(:conditions_met?, @post1.bullet_ar_key, :associations).should be_false
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "should be true when object is possible, and impossible" do
|
|
57
|
+
CounterCache.add_possible_objects(@post1)
|
|
58
|
+
CounterCache.add_impossible_object(@post1)
|
|
59
|
+
CounterCache.send(:conditions_met?, @post1.bullet_ar_key, :associations).should be_false
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -3,7 +3,10 @@ require 'spec_helper'
|
|
|
3
3
|
module Bullet
|
|
4
4
|
module Detector
|
|
5
5
|
describe NPlusOneQuery do
|
|
6
|
-
before(:all)
|
|
6
|
+
before(:all) do
|
|
7
|
+
@post = Post.first
|
|
8
|
+
@post2 = Post.last
|
|
9
|
+
end
|
|
7
10
|
before(:each) { NPlusOneQuery.clear }
|
|
8
11
|
|
|
9
12
|
context ".call_association" do
|
|
@@ -89,6 +92,25 @@ module Bullet
|
|
|
89
92
|
NPlusOneQuery.call_association(@post, :association)
|
|
90
93
|
end
|
|
91
94
|
end
|
|
95
|
+
|
|
96
|
+
context ".add_possible_objects" do
|
|
97
|
+
it "should add possible objects" do
|
|
98
|
+
NPlusOneQuery.add_possible_objects([@post, @post2])
|
|
99
|
+
NPlusOneQuery.send(:possible_objects).should be_include(@post.bullet_ar_key)
|
|
100
|
+
NPlusOneQuery.send(:possible_objects).should be_include(@post2.bullet_ar_key)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
it "should not raise error if object is nil" do
|
|
104
|
+
lambda { NPlusOneQuery.add_possible_objects(nil) }.should_not raise_error
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
context ".add_impossible_object" do
|
|
109
|
+
it "should add impossible object" do
|
|
110
|
+
NPlusOneQuery.add_impossible_object(@post)
|
|
111
|
+
NPlusOneQuery.send(:impossible_objects).should be_include(@post.bullet_ar_key)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
92
114
|
end
|
|
93
115
|
end
|
|
94
116
|
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
module Bullet
|
|
4
|
+
module Detector
|
|
5
|
+
describe UnusedEagerLoading do
|
|
6
|
+
before(:all) do
|
|
7
|
+
@post = Post.first
|
|
8
|
+
@post2 = Post.last
|
|
9
|
+
end
|
|
10
|
+
before(:each) { UnusedEagerLoading.clear }
|
|
11
|
+
|
|
12
|
+
context ".call_associations" do
|
|
13
|
+
it "should get empty array if eager_loadgins" do
|
|
14
|
+
UnusedEagerLoading.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "should get call associations if object and association are both in eager_loadings and call_object_associations" do
|
|
18
|
+
UnusedEagerLoading.add_eager_loadings([@post], :association)
|
|
19
|
+
UnusedEagerLoading.add_call_object_associations(@post, :association)
|
|
20
|
+
UnusedEagerLoading.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).should == [:association]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "should not get call associations if not exist in call_object_associations" do
|
|
24
|
+
UnusedEagerLoading.add_eager_loadings([@post], :association)
|
|
25
|
+
UnusedEagerLoading.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
context ".diff_object_associations" do
|
|
30
|
+
it "should return associations not exist in call_association" do
|
|
31
|
+
UnusedEagerLoading.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).should == [:association]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "should return empty if associations exist in call_association" do
|
|
35
|
+
UnusedEagerLoading.add_eager_loadings([@post], :association)
|
|
36
|
+
UnusedEagerLoading.add_call_object_associations(@post, :association)
|
|
37
|
+
UnusedEagerLoading.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
context ".check_unused_preload_associations" do
|
|
42
|
+
it "should set @@checked to true" do
|
|
43
|
+
UnusedEagerLoading.check_unused_preload_associations
|
|
44
|
+
UnusedEagerLoading.class_variable_get(:@@checked).should be_true
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "should create notification if object_association_diff is not empty" do
|
|
48
|
+
UnusedEagerLoading.add_object_associations(@post, :association)
|
|
49
|
+
UnusedEagerLoading.should_receive(:create_notification).with("Post", [:association])
|
|
50
|
+
UnusedEagerLoading.check_unused_preload_associations
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "should not create notification if object_association_diff is empty" do
|
|
54
|
+
UnusedEagerLoading.clear
|
|
55
|
+
UnusedEagerLoading.add_object_associations(@post, :association)
|
|
56
|
+
UnusedEagerLoading.add_eager_loadings([@post], :association)
|
|
57
|
+
UnusedEagerLoading.add_call_object_associations(@post, :association)
|
|
58
|
+
UnusedEagerLoading.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
|
|
59
|
+
UnusedEagerLoading.should_not_receive(:create_notification).with("Post", [:association])
|
|
60
|
+
UnusedEagerLoading.check_unused_preload_associations
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
context ".add_eager_loadings" do
|
|
65
|
+
it "should add objects, associations pair when eager_loadings are empty" do
|
|
66
|
+
UnusedEagerLoading.add_eager_loadings([@post, @post2], :associations)
|
|
67
|
+
UnusedEagerLoading.send(:eager_loadings).should be_include([@post.bullet_ar_key, @post2.bullet_ar_key], :associations)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "should add objects, associations pair for existing eager_loadings" do
|
|
71
|
+
UnusedEagerLoading.add_eager_loadings([@post, @post2], :association1)
|
|
72
|
+
UnusedEagerLoading.add_eager_loadings([@post, @post2], :association2)
|
|
73
|
+
UnusedEagerLoading.send(:eager_loadings).should be_include([@post.bullet_ar_key, @post2.bullet_ar_key], :association1)
|
|
74
|
+
UnusedEagerLoading.send(:eager_loadings).should be_include([@post.bullet_ar_key, @post2.bullet_ar_key], :association2)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "should merge objects, associations pair for existing eager_loadings" do
|
|
78
|
+
UnusedEagerLoading.add_eager_loadings([@post], :association1)
|
|
79
|
+
UnusedEagerLoading.add_eager_loadings([@post, @post2], :association2)
|
|
80
|
+
UnusedEagerLoading.send(:eager_loadings).should be_include([@post.bullet_ar_key], :association1)
|
|
81
|
+
UnusedEagerLoading.send(:eager_loadings).should be_include([@post.bullet_ar_key], :association2)
|
|
82
|
+
UnusedEagerLoading.send(:eager_loadings).should be_include([@post.bullet_ar_key, @post2.bullet_ar_key], :association2)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "should delete objects, associations pair for existing eager_loadings" do
|
|
86
|
+
UnusedEagerLoading.add_eager_loadings([@post, @post2], :association1)
|
|
87
|
+
UnusedEagerLoading.add_eager_loadings([@post], :association2)
|
|
88
|
+
UnusedEagerLoading.send(:eager_loadings).should be_include([@post.bullet_ar_key], :association1)
|
|
89
|
+
UnusedEagerLoading.send(:eager_loadings).should be_include([@post.bullet_ar_key], :association2)
|
|
90
|
+
UnusedEagerLoading.send(:eager_loadings).should be_include([@post2.bullet_ar_key], :association1)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|