bullet 5.5.0 → 5.8.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 +1 -5
- data/CHANGELOG.md +31 -1
- data/Gemfile +6 -1
- data/Gemfile.rails-5.0 +1 -1
- data/Gemfile.rails-5.1 +15 -0
- data/Gemfile.rails-5.2 +15 -0
- data/Guardfile +2 -2
- data/Hacking.md +1 -1
- data/README.md +20 -23
- data/Rakefile +20 -21
- data/bullet.gemspec +20 -15
- data/lib/bullet/active_record4.rb +18 -14
- data/lib/bullet/active_record41.rb +16 -14
- data/lib/bullet/active_record42.rb +24 -22
- data/lib/bullet/active_record5.rb +102 -91
- data/lib/bullet/active_record52.rb +232 -0
- data/lib/bullet/dependency.rb +9 -1
- data/lib/bullet/detector/association.rb +17 -14
- data/lib/bullet/detector/base.rb +2 -0
- data/lib/bullet/detector/counter_cache.rb +14 -10
- data/lib/bullet/detector/n_plus_one_query.rb +21 -17
- data/lib/bullet/detector/unused_eager_loading.rb +30 -24
- data/lib/bullet/detector.rb +2 -0
- data/lib/bullet/ext/object.rb +6 -4
- data/lib/bullet/ext/string.rb +3 -1
- data/lib/bullet/mongoid4x.rb +7 -2
- data/lib/bullet/mongoid5x.rb +7 -2
- data/lib/bullet/mongoid6x.rb +11 -6
- data/lib/bullet/notification/base.rb +26 -19
- data/lib/bullet/notification/counter_cache.rb +3 -1
- data/lib/bullet/notification/n_plus_one_query.rb +7 -4
- data/lib/bullet/notification/unused_eager_loading.rb +7 -4
- data/lib/bullet/notification.rb +2 -0
- data/lib/bullet/notification_collector.rb +2 -1
- data/lib/bullet/rack.rb +22 -18
- data/lib/bullet/registry/association.rb +2 -0
- data/lib/bullet/registry/base.rb +2 -0
- data/lib/bullet/registry/object.rb +2 -0
- data/lib/bullet/registry.rb +2 -0
- data/lib/bullet/stack_trace_filter.rb +56 -18
- data/lib/bullet/version.rb +3 -2
- data/lib/bullet.rb +43 -33
- data/lib/generators/bullet/install_generator.rb +48 -0
- data/perf/benchmark.rb +12 -12
- data/rails/init.rb +2 -0
- data/spec/bullet/detector/association_spec.rb +6 -4
- data/spec/bullet/detector/base_spec.rb +2 -0
- data/spec/bullet/detector/counter_cache_spec.rb +13 -11
- data/spec/bullet/detector/n_plus_one_query_spec.rb +71 -46
- data/spec/bullet/detector/unused_eager_loading_spec.rb +21 -19
- data/spec/bullet/ext/object_spec.rb +10 -8
- data/spec/bullet/ext/string_spec.rb +7 -5
- data/spec/bullet/notification/base_spec.rb +39 -38
- data/spec/bullet/notification/counter_cache_spec.rb +5 -3
- data/spec/bullet/notification/n_plus_one_query_spec.rb +5 -3
- data/spec/bullet/notification/unused_eager_loading_spec.rb +4 -2
- data/spec/bullet/notification_collector_spec.rb +12 -10
- data/spec/bullet/rack_spec.rb +48 -47
- data/spec/bullet/registry/association_spec.rb +12 -10
- data/spec/bullet/registry/base_spec.rb +22 -20
- data/spec/bullet/registry/object_spec.rb +6 -4
- data/spec/bullet_spec.rb +24 -3
- data/spec/integration/{active_record4 → active_record}/association_spec.rb +176 -114
- data/spec/integration/counter_cache_spec.rb +15 -13
- data/spec/integration/mongoid/association_spec.rb +35 -33
- data/spec/models/address.rb +2 -0
- data/spec/models/author.rb +2 -0
- data/spec/models/base_user.rb +2 -0
- data/spec/models/category.rb +2 -0
- data/spec/models/city.rb +2 -0
- data/spec/models/client.rb +4 -0
- data/spec/models/comment.rb +3 -1
- data/spec/models/company.rb +2 -0
- data/spec/models/country.rb +2 -0
- data/spec/models/document.rb +4 -2
- data/spec/models/entry.rb +2 -0
- data/spec/models/firm.rb +3 -0
- data/spec/models/folder.rb +2 -0
- data/spec/models/group.rb +4 -0
- data/spec/models/mongoid/address.rb +3 -1
- data/spec/models/mongoid/category.rb +4 -2
- data/spec/models/mongoid/comment.rb +3 -1
- data/spec/models/mongoid/company.rb +3 -1
- data/spec/models/mongoid/entry.rb +3 -1
- data/spec/models/mongoid/post.rb +6 -4
- data/spec/models/mongoid/user.rb +2 -0
- data/spec/models/newspaper.rb +3 -1
- data/spec/models/page.rb +2 -0
- data/spec/models/person.rb +2 -0
- data/spec/models/pet.rb +2 -0
- data/spec/models/post.rb +17 -0
- data/spec/models/relationship.rb +2 -0
- data/spec/models/reply.rb +2 -0
- data/spec/models/student.rb +2 -0
- data/spec/models/submission.rb +2 -0
- data/spec/models/teacher.rb +2 -0
- data/spec/models/user.rb +2 -0
- data/spec/models/writer.rb +2 -0
- data/spec/spec_helper.rb +15 -13
- data/spec/support/bullet_ext.rb +2 -0
- data/spec/support/mongo_seed.rb +36 -42
- data/spec/support/rack_double.rb +13 -19
- data/spec/support/sqlite_seed.rb +84 -76
- data/tasks/bullet_tasks.rake +4 -2
- data/test.sh +2 -0
- data/update.sh +2 -0
- metadata +17 -11
- data/spec/integration/active_record5/association_spec.rb +0 -768
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'spec_helper'
|
|
2
4
|
|
|
3
5
|
if !mongoid? && active_record?
|
|
@@ -10,26 +12,26 @@ if !mongoid? && active_record?
|
|
|
10
12
|
Bullet.end_request
|
|
11
13
|
end
|
|
12
14
|
|
|
13
|
-
it
|
|
15
|
+
it 'should need counter cache with all cities' do
|
|
14
16
|
Country.all.each do |country|
|
|
15
17
|
country.cities.size
|
|
16
18
|
end
|
|
17
19
|
expect(Bullet.collected_counter_cache_notifications).not_to be_empty
|
|
18
20
|
end
|
|
19
21
|
|
|
20
|
-
it
|
|
22
|
+
it 'should not need counter cache if already define counter_cache' do
|
|
21
23
|
Person.all.each do |person|
|
|
22
24
|
person.pets.size
|
|
23
25
|
end
|
|
24
26
|
expect(Bullet.collected_counter_cache_notifications).to be_empty
|
|
25
27
|
end
|
|
26
28
|
|
|
27
|
-
it
|
|
29
|
+
it 'should not need counter cache with only one object' do
|
|
28
30
|
Country.first.cities.size
|
|
29
31
|
expect(Bullet.collected_counter_cache_notifications).to be_empty
|
|
30
32
|
end
|
|
31
33
|
|
|
32
|
-
it
|
|
34
|
+
it 'should not need counter cache without size' do
|
|
33
35
|
Country.includes(:cities).each do |country|
|
|
34
36
|
country.cities.empty?
|
|
35
37
|
end
|
|
@@ -37,14 +39,14 @@ if !mongoid? && active_record?
|
|
|
37
39
|
end
|
|
38
40
|
|
|
39
41
|
if active_record5?
|
|
40
|
-
it
|
|
42
|
+
it 'should not need counter cache for has_many through' do
|
|
41
43
|
Client.all.each do |client|
|
|
42
44
|
client.firms.size
|
|
43
45
|
end
|
|
44
46
|
expect(Bullet.collected_counter_cache_notifications).to be_empty
|
|
45
47
|
end
|
|
46
48
|
else
|
|
47
|
-
it
|
|
49
|
+
it 'should need counter cache for has_many through' do
|
|
48
50
|
Client.all.each do |client|
|
|
49
51
|
client.firms.size
|
|
50
52
|
end
|
|
@@ -52,18 +54,18 @@ if !mongoid? && active_record?
|
|
|
52
54
|
end
|
|
53
55
|
end
|
|
54
56
|
|
|
55
|
-
it
|
|
57
|
+
it 'should not need counter cache with part of cities' do
|
|
56
58
|
Country.all.each do |country|
|
|
57
|
-
country.cities.where(:
|
|
59
|
+
country.cities.where(name: 'first').size
|
|
58
60
|
end
|
|
59
61
|
expect(Bullet.collected_counter_cache_notifications).to be_empty
|
|
60
62
|
end
|
|
61
63
|
|
|
62
|
-
context
|
|
64
|
+
context 'disable' do
|
|
63
65
|
before { Bullet.counter_cache_enable = false }
|
|
64
66
|
after { Bullet.counter_cache_enable = true }
|
|
65
67
|
|
|
66
|
-
it
|
|
68
|
+
it 'should not detect counter cache' do
|
|
67
69
|
Country.all.each do |country|
|
|
68
70
|
country.cities.size
|
|
69
71
|
end
|
|
@@ -71,11 +73,11 @@ if !mongoid? && active_record?
|
|
|
71
73
|
end
|
|
72
74
|
end
|
|
73
75
|
|
|
74
|
-
context
|
|
75
|
-
before { Bullet.add_whitelist :
|
|
76
|
+
context 'whitelist' do
|
|
77
|
+
before { Bullet.add_whitelist type: :counter_cache, class_name: 'Country', association: :cities }
|
|
76
78
|
after { Bullet.clear_whitelist }
|
|
77
79
|
|
|
78
|
-
it
|
|
80
|
+
it 'should not detect counter cache' do
|
|
79
81
|
Country.all.each do |country|
|
|
80
82
|
country.cities.size
|
|
81
83
|
end
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'spec_helper'
|
|
2
4
|
|
|
3
5
|
if mongoid?
|
|
4
6
|
describe Bullet::Detector::Association do
|
|
5
7
|
context 'embeds_many' do
|
|
6
|
-
context
|
|
7
|
-
it
|
|
8
|
+
context 'posts => users' do
|
|
9
|
+
it 'should detect nothing' do
|
|
8
10
|
Mongoid::Post.all.each do |post|
|
|
9
11
|
post.users.map(&:name)
|
|
10
12
|
end
|
|
@@ -17,8 +19,8 @@ if mongoid?
|
|
|
17
19
|
end
|
|
18
20
|
|
|
19
21
|
context 'has_many' do
|
|
20
|
-
context
|
|
21
|
-
it
|
|
22
|
+
context 'posts => comments' do
|
|
23
|
+
it 'should detect non preload posts => comments' do
|
|
22
24
|
Mongoid::Post.all.each do |post|
|
|
23
25
|
post.comments.map(&:name)
|
|
24
26
|
end
|
|
@@ -28,7 +30,7 @@ if mongoid?
|
|
|
28
30
|
expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Mongoid::Post, :comments)
|
|
29
31
|
end
|
|
30
32
|
|
|
31
|
-
it
|
|
33
|
+
it 'should detect preload post => comments' do
|
|
32
34
|
Mongoid::Post.includes(:comments).each do |post|
|
|
33
35
|
post.comments.map(&:name)
|
|
34
36
|
end
|
|
@@ -38,7 +40,7 @@ if mongoid?
|
|
|
38
40
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
|
39
41
|
end
|
|
40
42
|
|
|
41
|
-
it
|
|
43
|
+
it 'should detect unused preload post => comments' do
|
|
42
44
|
Mongoid::Post.includes(:comments).map(&:name)
|
|
43
45
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
|
44
46
|
expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Mongoid::Post, :comments)
|
|
@@ -46,7 +48,7 @@ if mongoid?
|
|
|
46
48
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
|
47
49
|
end
|
|
48
50
|
|
|
49
|
-
it
|
|
51
|
+
it 'should not detect unused preload post => comments' do
|
|
50
52
|
Mongoid::Post.all.map(&:name)
|
|
51
53
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
|
52
54
|
expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
|
|
@@ -55,8 +57,8 @@ if mongoid?
|
|
|
55
57
|
end
|
|
56
58
|
end
|
|
57
59
|
|
|
58
|
-
context
|
|
59
|
-
it
|
|
60
|
+
context 'category => posts, category => entries' do
|
|
61
|
+
it 'should detect non preload with category => [posts, entries]' do
|
|
60
62
|
Mongoid::Category.all.each do |category|
|
|
61
63
|
category.posts.map(&:name)
|
|
62
64
|
category.entries.map(&:name)
|
|
@@ -68,7 +70,7 @@ if mongoid?
|
|
|
68
70
|
expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Mongoid::Category, :entries)
|
|
69
71
|
end
|
|
70
72
|
|
|
71
|
-
it
|
|
73
|
+
it 'should detect preload with category => posts, but not with category => entries' do
|
|
72
74
|
Mongoid::Category.includes(:posts).each do |category|
|
|
73
75
|
category.posts.map(&:name)
|
|
74
76
|
category.entries.map(&:name)
|
|
@@ -80,7 +82,7 @@ if mongoid?
|
|
|
80
82
|
expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Mongoid::Category, :entries)
|
|
81
83
|
end
|
|
82
84
|
|
|
83
|
-
it
|
|
85
|
+
it 'should detect preload with category => [posts, entries]' do
|
|
84
86
|
Mongoid::Category.includes(:posts, :entries).each do |category|
|
|
85
87
|
category.posts.map(&:name)
|
|
86
88
|
category.entries.map(&:name)
|
|
@@ -91,7 +93,7 @@ if mongoid?
|
|
|
91
93
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
|
92
94
|
end
|
|
93
95
|
|
|
94
|
-
it
|
|
96
|
+
it 'should detect unused preload with category => [posts, entries]' do
|
|
95
97
|
Mongoid::Category.includes(:posts, :entries).map(&:name)
|
|
96
98
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
|
97
99
|
expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Mongoid::Category, :posts)
|
|
@@ -100,7 +102,7 @@ if mongoid?
|
|
|
100
102
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
|
101
103
|
end
|
|
102
104
|
|
|
103
|
-
it
|
|
105
|
+
it 'should detect unused preload with category => entries, but not with category => posts' do
|
|
104
106
|
Mongoid::Category.includes(:posts, :entries).each do |category|
|
|
105
107
|
category.posts.map(&:name)
|
|
106
108
|
end
|
|
@@ -112,8 +114,8 @@ if mongoid?
|
|
|
112
114
|
end
|
|
113
115
|
end
|
|
114
116
|
|
|
115
|
-
context
|
|
116
|
-
it
|
|
117
|
+
context 'post => comment' do
|
|
118
|
+
it 'should detect unused preload with post => comments' do
|
|
117
119
|
Mongoid::Post.includes(:comments).each do |post|
|
|
118
120
|
post.comments.first.name
|
|
119
121
|
end
|
|
@@ -123,7 +125,7 @@ if mongoid?
|
|
|
123
125
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
|
124
126
|
end
|
|
125
127
|
|
|
126
|
-
it
|
|
128
|
+
it 'should detect preload with post => commnets' do
|
|
127
129
|
Mongoid::Post.first.comments.map(&:name)
|
|
128
130
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
|
129
131
|
expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
|
|
@@ -132,8 +134,8 @@ if mongoid?
|
|
|
132
134
|
end
|
|
133
135
|
end
|
|
134
136
|
|
|
135
|
-
context
|
|
136
|
-
it
|
|
137
|
+
context 'scope preload_comments' do
|
|
138
|
+
it 'should detect preload post => comments with scope' do
|
|
137
139
|
Mongoid::Post.preload_comments.each do |post|
|
|
138
140
|
post.comments.map(&:name)
|
|
139
141
|
end
|
|
@@ -143,7 +145,7 @@ if mongoid?
|
|
|
143
145
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
|
144
146
|
end
|
|
145
147
|
|
|
146
|
-
it
|
|
148
|
+
it 'should detect unused preload with scope' do
|
|
147
149
|
Mongoid::Post.preload_comments.map(&:name)
|
|
148
150
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
|
149
151
|
expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Mongoid::Post, :comments)
|
|
@@ -154,8 +156,8 @@ if mongoid?
|
|
|
154
156
|
end
|
|
155
157
|
|
|
156
158
|
context 'belongs_to' do
|
|
157
|
-
context
|
|
158
|
-
it
|
|
159
|
+
context 'comment => post' do
|
|
160
|
+
it 'should detect non preload with comment => post' do
|
|
159
161
|
Mongoid::Comment.all.each do |comment|
|
|
160
162
|
comment.post.name
|
|
161
163
|
end
|
|
@@ -165,7 +167,7 @@ if mongoid?
|
|
|
165
167
|
expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Mongoid::Comment, :post)
|
|
166
168
|
end
|
|
167
169
|
|
|
168
|
-
it
|
|
170
|
+
it 'should detect preload with one comment => post' do
|
|
169
171
|
Mongoid::Comment.first.post.name
|
|
170
172
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
|
171
173
|
expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
|
|
@@ -173,7 +175,7 @@ if mongoid?
|
|
|
173
175
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
|
174
176
|
end
|
|
175
177
|
|
|
176
|
-
it
|
|
178
|
+
it 'should detect preload with comment => post' do
|
|
177
179
|
Mongoid::Comment.includes(:post).each do |comment|
|
|
178
180
|
comment.post.name
|
|
179
181
|
end
|
|
@@ -183,7 +185,7 @@ if mongoid?
|
|
|
183
185
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
|
184
186
|
end
|
|
185
187
|
|
|
186
|
-
it
|
|
188
|
+
it 'should not detect preload with comment => post' do
|
|
187
189
|
Mongoid::Comment.all.map(&:name)
|
|
188
190
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
|
189
191
|
expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
|
|
@@ -191,7 +193,7 @@ if mongoid?
|
|
|
191
193
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
|
192
194
|
end
|
|
193
195
|
|
|
194
|
-
it
|
|
196
|
+
it 'should detect unused preload with comments => post' do
|
|
195
197
|
Mongoid::Comment.includes(:post).map(&:name)
|
|
196
198
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
|
197
199
|
expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Mongoid::Comment, :post)
|
|
@@ -201,10 +203,10 @@ if mongoid?
|
|
|
201
203
|
end
|
|
202
204
|
end
|
|
203
205
|
|
|
204
|
-
context
|
|
205
|
-
context
|
|
206
|
+
context 'has_one' do
|
|
207
|
+
context 'company => address' do
|
|
206
208
|
if Mongoid::VERSION !~ /\A3.0/
|
|
207
|
-
it
|
|
209
|
+
it 'should detect non preload association' do
|
|
208
210
|
Mongoid::Company.all.each do |company|
|
|
209
211
|
company.address.name
|
|
210
212
|
end
|
|
@@ -215,7 +217,7 @@ if mongoid?
|
|
|
215
217
|
end
|
|
216
218
|
end
|
|
217
219
|
|
|
218
|
-
it
|
|
220
|
+
it 'should detect preload association' do
|
|
219
221
|
Mongoid::Company.includes(:address).each do |company|
|
|
220
222
|
company.address.name
|
|
221
223
|
end
|
|
@@ -225,7 +227,7 @@ if mongoid?
|
|
|
225
227
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
|
226
228
|
end
|
|
227
229
|
|
|
228
|
-
it
|
|
230
|
+
it 'should not detect preload association' do
|
|
229
231
|
Mongoid::Company.all.map(&:name)
|
|
230
232
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
|
231
233
|
expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
|
|
@@ -233,7 +235,7 @@ if mongoid?
|
|
|
233
235
|
expect(Bullet::Detector::Association).to be_completely_preloading_associations
|
|
234
236
|
end
|
|
235
237
|
|
|
236
|
-
it
|
|
238
|
+
it 'should detect unused preload association' do
|
|
237
239
|
criteria = Mongoid::Company.includes(:address)
|
|
238
240
|
criteria.map(&:name)
|
|
239
241
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
|
@@ -244,8 +246,8 @@ if mongoid?
|
|
|
244
246
|
end
|
|
245
247
|
end
|
|
246
248
|
|
|
247
|
-
context
|
|
248
|
-
it
|
|
249
|
+
context 'call one association that in possible objects' do
|
|
250
|
+
it 'should not detect preload association' do
|
|
249
251
|
Mongoid::Post.all
|
|
250
252
|
Mongoid::Post.first.comments.map(&:name)
|
|
251
253
|
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
|
data/spec/models/address.rb
CHANGED
data/spec/models/author.rb
CHANGED
data/spec/models/base_user.rb
CHANGED
data/spec/models/category.rb
CHANGED
data/spec/models/city.rb
CHANGED
data/spec/models/client.rb
CHANGED
data/spec/models/comment.rb
CHANGED
data/spec/models/company.rb
CHANGED
data/spec/models/country.rb
CHANGED
data/spec/models/document.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
class Document < ActiveRecord::Base
|
|
2
|
-
has_many :children, class_name:
|
|
3
|
-
belongs_to :parent, class_name:
|
|
4
|
+
has_many :children, class_name: 'Document', foreign_key: 'parent_id'
|
|
5
|
+
belongs_to :parent, class_name: 'Document', foreign_key: 'parent_id'
|
|
4
6
|
belongs_to :author
|
|
5
7
|
end
|
data/spec/models/entry.rb
CHANGED
data/spec/models/firm.rb
CHANGED
data/spec/models/folder.rb
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
class Mongoid::Category
|
|
2
4
|
include Mongoid::Document
|
|
3
5
|
|
|
4
6
|
field :name
|
|
5
7
|
|
|
6
|
-
has_many :posts, :
|
|
7
|
-
has_many :entries, :
|
|
8
|
+
has_many :posts, class_name: 'Mongoid::Post'
|
|
9
|
+
has_many :entries, class_name: 'Mongoid::Entry'
|
|
8
10
|
end
|
data/spec/models/mongoid/post.rb
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
class Mongoid::Post
|
|
2
4
|
include Mongoid::Document
|
|
3
5
|
|
|
4
6
|
field :name
|
|
5
7
|
|
|
6
|
-
has_many :comments, :
|
|
7
|
-
belongs_to :category, :
|
|
8
|
+
has_many :comments, class_name: 'Mongoid::Comment'
|
|
9
|
+
belongs_to :category, class_name: 'Mongoid::Category'
|
|
8
10
|
|
|
9
|
-
embeds_many :users, :
|
|
11
|
+
embeds_many :users, class_name: 'Mongoid::User'
|
|
10
12
|
|
|
11
|
-
scope :preload_comments,
|
|
13
|
+
scope :preload_comments, -> { includes(:comments) }
|
|
12
14
|
end
|
data/spec/models/mongoid/user.rb
CHANGED
data/spec/models/newspaper.rb
CHANGED
data/spec/models/page.rb
CHANGED
data/spec/models/person.rb
CHANGED
data/spec/models/pet.rb
CHANGED
data/spec/models/post.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
class Post < ActiveRecord::Base
|
|
2
4
|
belongs_to :category, inverse_of: :posts
|
|
3
5
|
belongs_to :writer
|
|
@@ -12,4 +14,19 @@ class Post < ActiveRecord::Base
|
|
|
12
14
|
def link=(*)
|
|
13
15
|
comments.new
|
|
14
16
|
end
|
|
17
|
+
|
|
18
|
+
# see association_spec.rb 'should not detect newly assigned object in an after_save'
|
|
19
|
+
attr_accessor :trigger_after_save
|
|
20
|
+
after_save do
|
|
21
|
+
next unless trigger_after_save
|
|
22
|
+
|
|
23
|
+
temp_comment = Comment.new(post: self)
|
|
24
|
+
# this triggers self to be "possible", even though it's
|
|
25
|
+
# not saved yet
|
|
26
|
+
temp_comment.post
|
|
27
|
+
|
|
28
|
+
# category should NOT whine about not being pre-loaded, because
|
|
29
|
+
# it's obviously attached to a new object
|
|
30
|
+
category
|
|
31
|
+
end
|
|
15
32
|
end
|
data/spec/models/relationship.rb
CHANGED
data/spec/models/reply.rb
CHANGED
data/spec/models/student.rb
CHANGED
data/spec/models/submission.rb
CHANGED
data/spec/models/teacher.rb
CHANGED
data/spec/models/user.rb
CHANGED
data/spec/models/writer.rb
CHANGED