bullet 4.0.0 → 4.13.1

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.
Files changed (112) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.rspec +1 -1
  4. data/.travis.yml +17 -6
  5. data/CHANGELOG.md +86 -0
  6. data/Gemfile +13 -12
  7. data/Gemfile.mongoid +14 -0
  8. data/Gemfile.mongoid-2.4 +19 -0
  9. data/Gemfile.mongoid-2.5 +19 -0
  10. data/Gemfile.mongoid-2.6 +19 -0
  11. data/Gemfile.mongoid-2.7 +19 -0
  12. data/Gemfile.mongoid-2.8 +19 -0
  13. data/Gemfile.mongoid-3.0 +19 -0
  14. data/Gemfile.mongoid-3.1 +19 -0
  15. data/Gemfile.mongoid-4.0 +19 -0
  16. data/Gemfile.rails-3.0 +19 -0
  17. data/Gemfile.rails-3.1 +19 -0
  18. data/Gemfile.rails-3.2 +19 -0
  19. data/Gemfile.rails-4.0 +19 -0
  20. data/Gemfile.rails-4.1 +19 -0
  21. data/{Hacking.textile → Hacking.md} +10 -5
  22. data/README.md +432 -0
  23. data/Rakefile +13 -6
  24. data/bullet.gemspec +6 -3
  25. data/lib/bullet/active_record3.rb +49 -11
  26. data/lib/bullet/active_record3x.rb +134 -0
  27. data/lib/bullet/{active_record31.rb → active_record4.rb} +45 -15
  28. data/lib/bullet/active_record41.rb +128 -0
  29. data/lib/bullet/dependency.rb +81 -0
  30. data/lib/bullet/detector/association.rb +24 -68
  31. data/lib/bullet/detector/base.rb +0 -6
  32. data/lib/bullet/detector/counter_cache.rb +59 -0
  33. data/lib/bullet/detector/n_plus_one_query.rb +56 -15
  34. data/lib/bullet/detector/unused_eager_loading.rb +84 -0
  35. data/lib/bullet/detector.rb +2 -2
  36. data/lib/bullet/ext/object.rb +10 -2
  37. data/lib/bullet/{mongoid.rb → mongoid2x.rb} +18 -18
  38. data/lib/bullet/mongoid3x.rb +56 -0
  39. data/lib/bullet/mongoid4x.rb +56 -0
  40. data/lib/bullet/notification/base.rb +22 -4
  41. data/lib/bullet/notification/n_plus_one_query.rb +1 -1
  42. data/lib/bullet/notification.rb +2 -0
  43. data/lib/bullet/rack.rb +51 -17
  44. data/lib/bullet/registry/base.rb +1 -1
  45. data/lib/bullet/registry/object.rb +4 -4
  46. data/lib/bullet/version.rb +1 -2
  47. data/lib/bullet.rb +119 -39
  48. data/perf/benchmark.rb +1 -4
  49. data/spec/bullet/detector/association_spec.rb +2 -70
  50. data/spec/bullet/detector/base_spec.rb +0 -6
  51. data/spec/bullet/detector/counter_cache_spec.rb +56 -0
  52. data/spec/bullet/detector/n_plus_one_query_spec.rb +78 -34
  53. data/spec/bullet/detector/unused_eager_loading_spec.rb +88 -0
  54. data/spec/bullet/ext/object_spec.rb +18 -7
  55. data/spec/bullet/ext/string_spec.rb +2 -2
  56. data/spec/bullet/notification/base_spec.rb +68 -15
  57. data/spec/bullet/notification/counter_cache_spec.rb +2 -2
  58. data/spec/bullet/notification/n_plus_one_query_spec.rb +4 -3
  59. data/spec/bullet/notification/unused_eager_loading_spec.rb +2 -2
  60. data/spec/bullet/notification_collector_spec.rb +4 -4
  61. data/spec/bullet/rack_spec.rb +40 -39
  62. data/spec/bullet/registry/association_spec.rb +3 -3
  63. data/spec/bullet/registry/base_spec.rb +6 -6
  64. data/spec/bullet/registry/object_spec.rb +4 -5
  65. data/spec/bullet_spec.rb +41 -0
  66. data/spec/integration/active_record3/association_spec.rb +662 -0
  67. data/spec/integration/active_record4/association_spec.rb +660 -0
  68. data/spec/integration/counter_cache_spec.rb +63 -0
  69. data/spec/integration/mongoid/association_spec.rb +190 -208
  70. data/spec/models/category.rb +1 -1
  71. data/spec/models/client.rb +1 -1
  72. data/spec/models/comment.rb +2 -2
  73. data/spec/models/document.rb +2 -2
  74. data/spec/models/firm.rb +1 -1
  75. data/spec/models/mongoid/address.rb +2 -0
  76. data/spec/models/mongoid/category.rb +2 -0
  77. data/spec/models/mongoid/comment.rb +2 -0
  78. data/spec/models/mongoid/company.rb +2 -0
  79. data/spec/models/mongoid/entry.rb +2 -0
  80. data/spec/models/mongoid/post.rb +4 -0
  81. data/spec/models/mongoid/user.rb +5 -0
  82. data/spec/models/newspaper.rb +1 -1
  83. data/spec/models/pet.rb +1 -1
  84. data/spec/models/post.rb +6 -13
  85. data/spec/spec_helper.rb +60 -25
  86. data/spec/support/bullet_ext.rb +1 -1
  87. data/spec/support/mongo_seed.rb +26 -2
  88. data/spec/support/sqlite_seed.rb +0 -6
  89. data/test.sh +15 -0
  90. metadata +77 -54
  91. data/.rvmrc +0 -2
  92. data/.rvmrc.example +0 -2
  93. data/Gemfile.lock +0 -132
  94. data/Gemfile.rails-2.3.14 +0 -16
  95. data/Gemfile.rails-2.3.14.lock +0 -65
  96. data/Gemfile.rails-3.0.12 +0 -17
  97. data/Gemfile.rails-3.0.12.lock +0 -116
  98. data/Gemfile.rails-3.1.4 +0 -18
  99. data/Gemfile.rails-3.1.4.lock +0 -134
  100. data/README.textile +0 -395
  101. data/README_for_rails2.textile +0 -400
  102. data/lib/bullet/action_controller2.rb +0 -49
  103. data/lib/bullet/active_record2.rb +0 -121
  104. data/lib/bullet/detector/counter.rb +0 -48
  105. data/lib/bullet/detector/unused_eager_association.rb +0 -42
  106. data/spec/bullet/detector/counter_spec.rb +0 -64
  107. data/spec/bullet/detector/unused_eager_association_spec.rb +0 -62
  108. data/spec/integration/association_spec.rb +0 -593
  109. data/spec/integration/counter_spec.rb +0 -39
  110. data/spec/integration/rails2/association_spec.rb +0 -593
  111. data/spec/integration/rails2/counter_spec.rb +0 -39
  112. data/test.result +0 -2293
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ if !mongoid? && active_record?
4
+ describe Bullet::Detector::CounterCache do
5
+ before(:each) do
6
+ Bullet.start_request
7
+ end
8
+
9
+ after(:each) do
10
+ Bullet.end_request
11
+ end
12
+
13
+ it "should need counter cache with all cities" do
14
+ Country.all.each do |country|
15
+ country.cities.size
16
+ end
17
+ expect(Bullet.collected_counter_cache_notifications).not_to be_empty
18
+ end
19
+
20
+ it "should not need counter cache if already define counter_cache" do
21
+ Person.all.each do |person|
22
+ person.pets.size
23
+ end
24
+ expect(Bullet.collected_counter_cache_notifications).to be_empty
25
+ end
26
+
27
+ it "should not need counter cache with only one object" do
28
+ Country.first.cities.size
29
+ expect(Bullet.collected_counter_cache_notifications).to be_empty
30
+ end
31
+
32
+ it "should not need counter cache with part of cities" do
33
+ Country.all.each do |country|
34
+ country.cities.where(:name => 'first').size
35
+ end
36
+ expect(Bullet.collected_counter_cache_notifications).to be_empty
37
+ end
38
+
39
+ context "disable" do
40
+ before { Bullet.counter_cache_enable = false }
41
+ after { Bullet.counter_cache_enable = true }
42
+
43
+ it "should not detect counter cache" do
44
+ Country.all.each do |country|
45
+ country.cities.size
46
+ end
47
+ expect(Bullet.collected_counter_cache_notifications).to be_empty
48
+ end
49
+ end
50
+
51
+ context "whitelist" do
52
+ before { Bullet.add_whitelist :type => :counter_cache, :class_name => "Country", :association => :cities }
53
+ after { Bullet.reset_whitelist }
54
+
55
+ it "should not detect counter cache" do
56
+ Country.all.each do |country|
57
+ country.cities.size
58
+ end
59
+ expect(Bullet.collected_counter_cache_notifications).to be_empty
60
+ end
61
+ end
62
+ end
63
+ end
@@ -1,276 +1,258 @@
1
1
  require 'spec_helper'
2
2
 
3
- begin
4
- require 'mongoid'
5
- describe Bullet::Detector::Association, 'has_many' do
6
- before(:each) do
7
- Bullet.clear
8
- Bullet.start_request
9
- end
10
- after(:each) do
11
- Bullet.end_request
3
+ if mongoid?
4
+ describe Bullet::Detector::Association do
5
+ context 'embeds_many' do
6
+ context "posts => users" do
7
+ it "should detect nothing" do
8
+ Mongoid::Post.all.each do |post|
9
+ post.users.map(&:name)
10
+ end
11
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
12
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
13
+
14
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
15
+ end
16
+ end
12
17
  end
13
18
 
14
- context "posts => comments" do
15
- it "should detect non preload posts => comments" do
16
- Mongoid::Post.all.each do |post|
17
- post.comments.map(&:name)
19
+ context 'has_many' do
20
+ context "posts => comments" do
21
+ it "should detect non preload posts => comments" do
22
+ Mongoid::Post.all.each do |post|
23
+ post.comments.map(&:name)
24
+ end
25
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
26
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
27
+
28
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Mongoid::Post, :comments)
18
29
  end
19
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
20
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
21
30
 
22
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Post, :comments)
23
- end
31
+ it "should detect preload post => comments" do
32
+ Mongoid::Post.includes(:comments).each do |post|
33
+ post.comments.map(&:name)
34
+ end
35
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
36
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
24
37
 
25
- it "should detect preload post => comments" do
26
- Mongoid::Post.includes(:comments).each do |post|
27
- post.comments.map(&:name)
38
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
28
39
  end
29
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
30
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
31
-
32
- Bullet::Detector::Association.should be_completely_preloading_associations
33
- end
34
40
 
35
- it "should detect unused preload post => comments" do
36
- Mongoid::Post.includes(:comments).map(&:name)
37
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
38
- Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Post, :comments)
41
+ it "should detect unused preload post => comments" do
42
+ Mongoid::Post.includes(:comments).map(&:name)
43
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
44
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Mongoid::Post, :comments)
39
45
 
40
- Bullet::Detector::Association.should be_completely_preloading_associations
41
- end
46
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
47
+ end
42
48
 
43
- it "should not detect unused preload post => comments" do
44
- Mongoid::Post.all.map(&:name)
45
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
46
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
49
+ it "should not detect unused preload post => comments" do
50
+ Mongoid::Post.all.map(&:name)
51
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
52
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
47
53
 
48
- Bullet::Detector::Association.should be_completely_preloading_associations
54
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
55
+ end
49
56
  end
50
- end
51
57
 
52
- context "category => posts, category => entries" do
53
- it "should detect non preload with category => [posts, entries]" do
54
- Mongoid::Category.all.each do |category|
55
- category.posts.map(&:name)
56
- category.entries.map(&:name)
58
+ context "category => posts, category => entries" do
59
+ it "should detect non preload with category => [posts, entries]" do
60
+ Mongoid::Category.all.each do |category|
61
+ category.posts.map(&:name)
62
+ category.entries.map(&:name)
63
+ end
64
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
65
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
66
+
67
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Mongoid::Category, :posts)
68
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Mongoid::Category, :entries)
57
69
  end
58
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
59
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
60
70
 
61
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Category, :posts)
62
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Category, :entries)
63
- end
71
+ it "should detect preload with category => posts, but not with category => entries" do
72
+ Mongoid::Category.includes(:posts).each do |category|
73
+ category.posts.map(&:name)
74
+ category.entries.map(&:name)
75
+ end
76
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
77
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
64
78
 
65
- it "should detect preload with category => posts, but not with category => entries" do
66
- Mongoid::Category.includes(:posts).each do |category|
67
- category.posts.collect(&:name)
68
- category.entries.collect(&:name)
79
+ expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Mongoid::Category, :posts)
80
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Mongoid::Category, :entries)
69
81
  end
70
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
71
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
72
82
 
73
- Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Mongoid::Category, :posts)
74
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Category, :entries)
75
- end
83
+ it "should detect preload with category => [posts, entries]" do
84
+ Mongoid::Category.includes(:posts, :entries).each do |category|
85
+ category.posts.map(&:name)
86
+ category.entries.map(&:name)
87
+ end
88
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
89
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
76
90
 
77
- it "should detect preload with category => [posts, entries]" do
78
- Mongoid::Category.includes([:posts, :entries]).each do |category|
79
- category.posts.map(&:name)
80
- category.entries.map(&:name)
91
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
81
92
  end
82
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
83
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
84
93
 
85
- Bullet::Detector::Association.should be_completely_preloading_associations
86
- end
94
+ it "should detect unused preload with category => [posts, entries]" do
95
+ Mongoid::Category.includes(:posts, :entries).map(&:name)
96
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
97
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Mongoid::Category, :posts)
98
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Mongoid::Category, :entries)
87
99
 
88
- it "should detect unused preload with category => [posts, entries]" do
89
- Mongoid::Category.includes([:posts, :entries]).map(&:name)
90
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
91
- Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Category, :posts)
92
- Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Category, :entries)
100
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
101
+ end
93
102
 
94
- Bullet::Detector::Association.should be_completely_preloading_associations
95
- end
103
+ it "should detect unused preload with category => entries, but not with category => posts" do
104
+ Mongoid::Category.includes(:posts, :entries).each do |category|
105
+ category.posts.map(&:name)
106
+ end
107
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
108
+ expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Mongoid::Category, :posts)
109
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Mongoid::Category, :entries)
96
110
 
97
- it "should detect unused preload with category => entries, but not with category => posts" do
98
- Mongoid::Category.includes([:posts, :entries]).each do |category|
99
- category.posts.map(&:name)
111
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
100
112
  end
101
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
102
- Bullet::Detector::Association.should_not be_unused_preload_associations_for(Mongoid::Category, :posts)
103
- Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Category, :entries)
104
-
105
- Bullet::Detector::Association.should be_completely_preloading_associations
106
113
  end
107
- end
108
114
 
109
- context "post => comment" do
110
- it "should detect unused preload with post => comments" do
111
- Mongoid::Post.includes(:comments).each do |post|
112
- post.comments.first.name
113
- end
114
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
115
- Bullet::Detector::Association.should_not be_unused_preload_associations_for(Mongoid::Post, :comments)
115
+ context "post => comment" do
116
+ it "should detect unused preload with post => comments" do
117
+ Mongoid::Post.includes(:comments).each do |post|
118
+ post.comments.first.name
119
+ end
120
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
121
+ expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Mongoid::Post, :comments)
116
122
 
117
- Bullet::Detector::Association.should be_completely_preloading_associations
118
- end
123
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
124
+ end
119
125
 
120
- it "should detect preload with post => commnets" do
121
- Mongoid::Post.first.comments.collect(&:name)
122
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
123
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
126
+ it "should detect preload with post => commnets" do
127
+ Mongoid::Post.first.comments.map(&:name)
128
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
129
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
124
130
 
125
- Bullet::Detector::Association.should be_completely_preloading_associations
131
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
132
+ end
126
133
  end
127
- end
128
134
 
129
- context "scope preload_comments" do
130
- it "should detect preload post => comments with scope" do
131
- Mongoid::Post.preload_comments.each do |post|
132
- post.comments.map(&:name)
133
- end
134
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
135
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
135
+ context "scope preload_comments" do
136
+ it "should detect preload post => comments with scope" do
137
+ Mongoid::Post.preload_comments.each do |post|
138
+ post.comments.map(&:name)
139
+ end
140
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
141
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
136
142
 
137
- Bullet::Detector::Association.should be_completely_preloading_associations
138
- end
143
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
144
+ end
139
145
 
140
- it "should detect unused preload with scope" do
141
- Mongoid::Post.preload_comments.map(&:name)
142
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
143
- Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Post, :comments)
146
+ it "should detect unused preload with scope" do
147
+ Mongoid::Post.preload_comments.map(&:name)
148
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
149
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Mongoid::Post, :comments)
144
150
 
145
- Bullet::Detector::Association.should be_completely_preloading_associations
151
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
152
+ end
146
153
  end
147
154
  end
148
- end
149
-
150
- describe Bullet::Detector::Association, 'belongs_to' do
151
- before(:each) do
152
- Bullet.clear
153
- Bullet.start_request
154
- end
155
155
 
156
- after(:each) do
157
- Bullet.end_request
158
- end
156
+ context 'belongs_to' do
157
+ context "comment => post" do
158
+ it "should detect non preload with comment => post" do
159
+ Mongoid::Comment.all.each do |comment|
160
+ comment.post.name
161
+ end
162
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
163
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
159
164
 
160
- context "comment => post" do
161
- it "should detect non preload with comment => post" do
162
- Mongoid::Comment.all.each do |comment|
163
- comment.post.name
165
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Mongoid::Comment, :post)
164
166
  end
165
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
166
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
167
167
 
168
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Comment, :post)
169
- end
168
+ it "should detect preload with one comment => post" do
169
+ Mongoid::Comment.first.post.name
170
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
171
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
170
172
 
171
- it "should detect preload with one comment => post" do
172
- Mongoid::Comment.first.post.name
173
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
174
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
173
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
174
+ end
175
175
 
176
- Bullet::Detector::Association.should be_completely_preloading_associations
177
- end
176
+ it "should detect preload with comment => post" do
177
+ Mongoid::Comment.includes(:post).each do |comment|
178
+ comment.post.name
179
+ end
180
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
181
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
178
182
 
179
- it "should detect preload with comment => post" do
180
- Mongoid::Comment.includes(:post).each do |comment|
181
- comment.post.name
183
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
182
184
  end
183
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
184
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
185
-
186
- Bullet::Detector::Association.should be_completely_preloading_associations
187
- end
188
185
 
189
- it "should not detect preload with comment => post" do
190
- Mongoid::Comment.all.collect(&:name)
191
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
192
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
186
+ it "should not detect preload with comment => post" do
187
+ Mongoid::Comment.all.map(&:name)
188
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
189
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
193
190
 
194
- Bullet::Detector::Association.should be_completely_preloading_associations
195
- end
191
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
192
+ end
196
193
 
197
- it "should detect unused preload with comments => post" do
198
- Mongoid::Comment.includes(:post).map(&:name)
199
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
200
- Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Comment, :post)
194
+ it "should detect unused preload with comments => post" do
195
+ Mongoid::Comment.includes(:post).map(&:name)
196
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
197
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Mongoid::Comment, :post)
201
198
 
202
- Bullet::Detector::Association.should be_completely_preloading_associations
199
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
200
+ end
203
201
  end
204
202
  end
205
- end
206
-
207
- describe Bullet::Detector::Association, "has_one" do
208
- before(:each) do
209
- Bullet.clear
210
- Bullet.start_request
211
- end
212
-
213
- after(:each) do
214
- Bullet.end_request
215
- end
216
203
 
217
- context "company => address" do
218
- it "should detect non preload association" do
219
- Mongoid::Company.all.each do |company|
220
- company.address.name
204
+ context "has_one" do
205
+ context "company => address" do
206
+ if Mongoid::VERSION !~ /\A3.0/
207
+ it "should detect non preload association" do
208
+ Mongoid::Company.all.each do |company|
209
+ company.address.name
210
+ end
211
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
212
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
213
+
214
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Mongoid::Company, :address)
215
+ end
221
216
  end
222
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
223
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
224
217
 
225
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Company, :address)
226
- end
218
+ it "should detect preload association" do
219
+ Mongoid::Company.includes(:address).each do |company|
220
+ company.address.name
221
+ end
222
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
223
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
227
224
 
228
- it "should detect preload association" do
229
- Mongoid::Company.includes(:address).each do |company|
230
- company.address.name
225
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
231
226
  end
232
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
233
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
234
-
235
- Bullet::Detector::Association.should be_completely_preloading_associations
236
- end
237
227
 
238
- it "should not detect preload association" do
239
- Mongoid::Company.all.collect(&:name)
240
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
241
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
228
+ it "should not detect preload association" do
229
+ Mongoid::Company.all.map(&:name)
230
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
231
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
242
232
 
243
- Bullet::Detector::Association.should be_completely_preloading_associations
244
- end
233
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
234
+ end
245
235
 
246
- it "should detect unused preload association" do
247
- Mongoid::Company.includes(:address).collect(&:name)
248
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
249
- Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Company, :address)
236
+ it "should detect unused preload association" do
237
+ criteria = Mongoid::Company.includes(:address)
238
+ criteria.map(&:name)
239
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
240
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Mongoid::Company, :address)
250
241
 
251
- Bullet::Detector::Association.should be_completely_preloading_associations
242
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
243
+ end
252
244
  end
253
245
  end
254
- end
255
-
256
- describe Bullet::Detector::Association, "call one association that in possible objects" do
257
- before(:each) do
258
- Bullet.clear
259
- Bullet.start_request
260
- end
261
246
 
262
- after(:each) do
263
- Bullet.end_request
264
- end
265
-
266
- it "should not detect preload association" do
267
- Mongoid::Post.all
268
- Mongoid::Post.first.comments.map(&:name)
269
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
270
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
247
+ context "call one association that in possible objects" do
248
+ it "should not detect preload association" do
249
+ Mongoid::Post.all
250
+ Mongoid::Post.first.comments.map(&:name)
251
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
252
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
271
253
 
272
- Bullet::Detector::Association.should be_completely_preloading_associations
254
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
255
+ end
273
256
  end
274
257
  end
275
- rescue LoadError
276
258
  end
@@ -1,5 +1,5 @@
1
1
  class Category < ActiveRecord::Base
2
- has_many :posts
2
+ has_many :posts, inverse_of: :category
3
3
  has_many :entries
4
4
 
5
5
  has_many :submissions
@@ -1,4 +1,4 @@
1
1
  class Client < ActiveRecord::Base
2
2
  has_many :relationships
3
- has_many :firms, :through => :relationships
3
+ has_many :firms, through: :relationships
4
4
  end
@@ -1,4 +1,4 @@
1
1
  class Comment < ActiveRecord::Base
2
- belongs_to :post
3
- belongs_to :author, :class_name => "BaseUser"
2
+ belongs_to :post, inverse_of: :comments
3
+ belongs_to :author, class_name: "BaseUser"
4
4
  end
@@ -1,5 +1,5 @@
1
1
  class Document < ActiveRecord::Base
2
- has_many :children, :class_name => "Document", :foreign_key => 'parent_id'
3
- belongs_to :parent, :class_name => "Document", :foreign_key => 'parent_id'
2
+ has_many :children, class_name: "Document", foreign_key: 'parent_id'
3
+ belongs_to :parent, class_name: "Document", foreign_key: 'parent_id'
4
4
  belongs_to :author
5
5
  end
data/spec/models/firm.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  class Firm < ActiveRecord::Base
2
2
  has_many :relationships
3
- has_many :clients, :through => :relationships
3
+ has_many :clients, through: :relationships
4
4
  end
@@ -1,5 +1,7 @@
1
1
  class Mongoid::Address
2
2
  include Mongoid::Document
3
3
 
4
+ field :name
5
+
4
6
  belongs_to :company, :class_name => "Mongoid::Company"
5
7
  end
@@ -1,6 +1,8 @@
1
1
  class Mongoid::Category
2
2
  include Mongoid::Document
3
3
 
4
+ field :name
5
+
4
6
  has_many :posts, :class_name => "Mongoid::Post"
5
7
  has_many :entries, :class_name => "Mongoid::Entry"
6
8
  end
@@ -1,5 +1,7 @@
1
1
  class Mongoid::Comment
2
2
  include Mongoid::Document
3
3
 
4
+ field :name
5
+
4
6
  belongs_to :post, :class_name => "Mongoid::Post"
5
7
  end
@@ -1,5 +1,7 @@
1
1
  class Mongoid::Company
2
2
  include Mongoid::Document
3
3
 
4
+ field :name
5
+
4
6
  has_one :address, :class_name => "Mongoid::Address"
5
7
  end
@@ -1,5 +1,7 @@
1
1
  class Mongoid::Entry
2
2
  include Mongoid::Document
3
3
 
4
+ field :name
5
+
4
6
  belongs_to :category, :class_name => "Mongoid::Category"
5
7
  end
@@ -1,8 +1,12 @@
1
1
  class Mongoid::Post
2
2
  include Mongoid::Document
3
3
 
4
+ field :name
5
+
4
6
  has_many :comments, :class_name => "Mongoid::Comment"
5
7
  belongs_to :category, :class_name => "Mongoid::Category"
6
8
 
9
+ embeds_many :users, :class_name => "Mongoid::User"
10
+
7
11
  scope :preload_comments, lambda { includes(:comments) }
8
12
  end
@@ -0,0 +1,5 @@
1
+ class Mongoid::User
2
+ include Mongoid::Document
3
+
4
+ field :name
5
+ end
@@ -1,3 +1,3 @@
1
1
  class Newspaper < ActiveRecord::Base
2
- has_many :writers, :class_name => "BaseUser"
2
+ has_many :writers, class_name: "BaseUser"
3
3
  end
data/spec/models/pet.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Pet < ActiveRecord::Base
2
- belongs_to :person, :counter_cache => true
2
+ belongs_to :person, counter_cache: true
3
3
  end