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.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -5
  3. data/CHANGELOG.md +31 -1
  4. data/Gemfile +6 -1
  5. data/Gemfile.rails-5.0 +1 -1
  6. data/Gemfile.rails-5.1 +15 -0
  7. data/Gemfile.rails-5.2 +15 -0
  8. data/Guardfile +2 -2
  9. data/Hacking.md +1 -1
  10. data/README.md +20 -23
  11. data/Rakefile +20 -21
  12. data/bullet.gemspec +20 -15
  13. data/lib/bullet/active_record4.rb +18 -14
  14. data/lib/bullet/active_record41.rb +16 -14
  15. data/lib/bullet/active_record42.rb +24 -22
  16. data/lib/bullet/active_record5.rb +102 -91
  17. data/lib/bullet/active_record52.rb +232 -0
  18. data/lib/bullet/dependency.rb +9 -1
  19. data/lib/bullet/detector/association.rb +17 -14
  20. data/lib/bullet/detector/base.rb +2 -0
  21. data/lib/bullet/detector/counter_cache.rb +14 -10
  22. data/lib/bullet/detector/n_plus_one_query.rb +21 -17
  23. data/lib/bullet/detector/unused_eager_loading.rb +30 -24
  24. data/lib/bullet/detector.rb +2 -0
  25. data/lib/bullet/ext/object.rb +6 -4
  26. data/lib/bullet/ext/string.rb +3 -1
  27. data/lib/bullet/mongoid4x.rb +7 -2
  28. data/lib/bullet/mongoid5x.rb +7 -2
  29. data/lib/bullet/mongoid6x.rb +11 -6
  30. data/lib/bullet/notification/base.rb +26 -19
  31. data/lib/bullet/notification/counter_cache.rb +3 -1
  32. data/lib/bullet/notification/n_plus_one_query.rb +7 -4
  33. data/lib/bullet/notification/unused_eager_loading.rb +7 -4
  34. data/lib/bullet/notification.rb +2 -0
  35. data/lib/bullet/notification_collector.rb +2 -1
  36. data/lib/bullet/rack.rb +22 -18
  37. data/lib/bullet/registry/association.rb +2 -0
  38. data/lib/bullet/registry/base.rb +2 -0
  39. data/lib/bullet/registry/object.rb +2 -0
  40. data/lib/bullet/registry.rb +2 -0
  41. data/lib/bullet/stack_trace_filter.rb +56 -18
  42. data/lib/bullet/version.rb +3 -2
  43. data/lib/bullet.rb +43 -33
  44. data/lib/generators/bullet/install_generator.rb +48 -0
  45. data/perf/benchmark.rb +12 -12
  46. data/rails/init.rb +2 -0
  47. data/spec/bullet/detector/association_spec.rb +6 -4
  48. data/spec/bullet/detector/base_spec.rb +2 -0
  49. data/spec/bullet/detector/counter_cache_spec.rb +13 -11
  50. data/spec/bullet/detector/n_plus_one_query_spec.rb +71 -46
  51. data/spec/bullet/detector/unused_eager_loading_spec.rb +21 -19
  52. data/spec/bullet/ext/object_spec.rb +10 -8
  53. data/spec/bullet/ext/string_spec.rb +7 -5
  54. data/spec/bullet/notification/base_spec.rb +39 -38
  55. data/spec/bullet/notification/counter_cache_spec.rb +5 -3
  56. data/spec/bullet/notification/n_plus_one_query_spec.rb +5 -3
  57. data/spec/bullet/notification/unused_eager_loading_spec.rb +4 -2
  58. data/spec/bullet/notification_collector_spec.rb +12 -10
  59. data/spec/bullet/rack_spec.rb +48 -47
  60. data/spec/bullet/registry/association_spec.rb +12 -10
  61. data/spec/bullet/registry/base_spec.rb +22 -20
  62. data/spec/bullet/registry/object_spec.rb +6 -4
  63. data/spec/bullet_spec.rb +24 -3
  64. data/spec/integration/{active_record4 → active_record}/association_spec.rb +176 -114
  65. data/spec/integration/counter_cache_spec.rb +15 -13
  66. data/spec/integration/mongoid/association_spec.rb +35 -33
  67. data/spec/models/address.rb +2 -0
  68. data/spec/models/author.rb +2 -0
  69. data/spec/models/base_user.rb +2 -0
  70. data/spec/models/category.rb +2 -0
  71. data/spec/models/city.rb +2 -0
  72. data/spec/models/client.rb +4 -0
  73. data/spec/models/comment.rb +3 -1
  74. data/spec/models/company.rb +2 -0
  75. data/spec/models/country.rb +2 -0
  76. data/spec/models/document.rb +4 -2
  77. data/spec/models/entry.rb +2 -0
  78. data/spec/models/firm.rb +3 -0
  79. data/spec/models/folder.rb +2 -0
  80. data/spec/models/group.rb +4 -0
  81. data/spec/models/mongoid/address.rb +3 -1
  82. data/spec/models/mongoid/category.rb +4 -2
  83. data/spec/models/mongoid/comment.rb +3 -1
  84. data/spec/models/mongoid/company.rb +3 -1
  85. data/spec/models/mongoid/entry.rb +3 -1
  86. data/spec/models/mongoid/post.rb +6 -4
  87. data/spec/models/mongoid/user.rb +2 -0
  88. data/spec/models/newspaper.rb +3 -1
  89. data/spec/models/page.rb +2 -0
  90. data/spec/models/person.rb +2 -0
  91. data/spec/models/pet.rb +2 -0
  92. data/spec/models/post.rb +17 -0
  93. data/spec/models/relationship.rb +2 -0
  94. data/spec/models/reply.rb +2 -0
  95. data/spec/models/student.rb +2 -0
  96. data/spec/models/submission.rb +2 -0
  97. data/spec/models/teacher.rb +2 -0
  98. data/spec/models/user.rb +2 -0
  99. data/spec/models/writer.rb +2 -0
  100. data/spec/spec_helper.rb +15 -13
  101. data/spec/support/bullet_ext.rb +2 -0
  102. data/spec/support/mongo_seed.rb +36 -42
  103. data/spec/support/rack_double.rb +13 -19
  104. data/spec/support/sqlite_seed.rb +84 -76
  105. data/tasks/bullet_tasks.rake +4 -2
  106. data/test.sh +2 -0
  107. data/update.sh +2 -0
  108. metadata +17 -11
  109. 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 "should need counter cache with all cities" do
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 "should not need counter cache if already define counter_cache" do
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 "should not need counter cache with only one object" do
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 "should not need counter cache without size" do
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 "should not need counter cache for has_many through" do
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 "should need counter cache for has_many through" do
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 "should not need counter cache with part of cities" do
57
+ it 'should not need counter cache with part of cities' do
56
58
  Country.all.each do |country|
57
- country.cities.where(:name => 'first').size
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 "disable" do
64
+ context 'disable' do
63
65
  before { Bullet.counter_cache_enable = false }
64
66
  after { Bullet.counter_cache_enable = true }
65
67
 
66
- it "should not detect counter cache" do
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 "whitelist" do
75
- before { Bullet.add_whitelist :type => :counter_cache, :class_name => "Country", :association => :cities }
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 "should not detect counter cache" do
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 "posts => users" do
7
- it "should detect nothing" do
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 "posts => comments" do
21
- it "should detect non preload posts => comments" do
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 "should detect preload post => comments" do
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 "should detect unused preload post => comments" do
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 "should not detect unused preload post => comments" do
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 "category => posts, category => entries" do
59
- it "should detect non preload with category => [posts, entries]" do
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 "should detect preload with category => posts, but not with category => entries" do
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 "should detect preload with category => [posts, entries]" do
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 "should detect unused preload with category => [posts, entries]" do
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 "should detect unused preload with category => entries, but not with category => posts" do
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 "post => comment" do
116
- it "should detect unused preload with post => comments" do
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 "should detect preload with post => commnets" do
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 "scope preload_comments" do
136
- it "should detect preload post => comments with scope" do
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 "should detect unused preload with scope" do
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 "comment => post" do
158
- it "should detect non preload with comment => post" do
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 "should detect preload with one comment => post" do
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 "should detect preload with comment => post" do
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 "should not detect preload with comment => post" do
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 "should detect unused preload with comments => post" do
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 "has_one" do
205
- context "company => address" do
206
+ context 'has_one' do
207
+ context 'company => address' do
206
208
  if Mongoid::VERSION !~ /\A3.0/
207
- it "should detect non preload association" do
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 "should detect preload association" do
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 "should not detect preload association" do
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 "should detect unused preload association" do
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 "call one association that in possible objects" do
248
- it "should not detect preload association" do
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Address < ActiveRecord::Base
2
4
  belongs_to :company
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Author < ActiveRecord::Base
2
4
  has_many :documents
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class BaseUser < ActiveRecord::Base
2
4
  has_many :comments
3
5
  has_many :posts
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Category < ActiveRecord::Base
2
4
  has_many :posts, inverse_of: :category
3
5
  has_many :entries
data/spec/models/city.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class City < ActiveRecord::Base
2
4
  belongs_to :country
3
5
  end
@@ -1,4 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Client < ActiveRecord::Base
4
+ belongs_to :group
5
+
2
6
  has_many :relationships
3
7
  has_many :firms, through: :relationships
4
8
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Comment < ActiveRecord::Base
2
4
  belongs_to :post, inverse_of: :comments
3
- belongs_to :author, class_name: "BaseUser"
5
+ belongs_to :author, class_name: 'BaseUser'
4
6
 
5
7
  validates :post, presence: true
6
8
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Company < ActiveRecord::Base
2
4
  has_one :address
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Country < ActiveRecord::Base
2
4
  has_many :cities
3
5
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  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'
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Entry < ActiveRecord::Base
2
4
  belongs_to :category
3
5
  end
data/spec/models/firm.rb CHANGED
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Firm < ActiveRecord::Base
2
4
  has_many :relationships
3
5
  has_many :clients, through: :relationships
6
+ has_many :groups, through: :clients
4
7
  end
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Folder < Document
2
4
  end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Group < ActiveRecord::Base
4
+ end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Mongoid::Address
2
4
  include Mongoid::Document
3
5
 
4
6
  field :name
5
7
 
6
- belongs_to :company, :class_name => "Mongoid::Company"
8
+ belongs_to :company, class_name: 'Mongoid::Company'
7
9
  end
@@ -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, :class_name => "Mongoid::Post"
7
- has_many :entries, :class_name => "Mongoid::Entry"
8
+ has_many :posts, class_name: 'Mongoid::Post'
9
+ has_many :entries, class_name: 'Mongoid::Entry'
8
10
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Mongoid::Comment
2
4
  include Mongoid::Document
3
5
 
4
6
  field :name
5
7
 
6
- belongs_to :post, :class_name => "Mongoid::Post"
8
+ belongs_to :post, class_name: 'Mongoid::Post'
7
9
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Mongoid::Company
2
4
  include Mongoid::Document
3
5
 
4
6
  field :name
5
7
 
6
- has_one :address, :class_name => "Mongoid::Address"
8
+ has_one :address, class_name: 'Mongoid::Address'
7
9
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Mongoid::Entry
2
4
  include Mongoid::Document
3
5
 
4
6
  field :name
5
7
 
6
- belongs_to :category, :class_name => "Mongoid::Category"
8
+ belongs_to :category, class_name: 'Mongoid::Category'
7
9
  end
@@ -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, :class_name => "Mongoid::Comment"
7
- belongs_to :category, :class_name => "Mongoid::Category"
8
+ has_many :comments, class_name: 'Mongoid::Comment'
9
+ belongs_to :category, class_name: 'Mongoid::Category'
8
10
 
9
- embeds_many :users, :class_name => "Mongoid::User"
11
+ embeds_many :users, class_name: 'Mongoid::User'
10
12
 
11
- scope :preload_comments, lambda { includes(:comments) }
13
+ scope :preload_comments, -> { includes(:comments) }
12
14
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Mongoid::User
2
4
  include Mongoid::Document
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Newspaper < ActiveRecord::Base
2
- has_many :writers, class_name: "BaseUser"
4
+ has_many :writers, class_name: 'BaseUser'
3
5
  end
data/spec/models/page.rb CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Page < Document
2
4
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Person < ActiveRecord::Base
2
4
  has_many :pets
3
5
  end
data/spec/models/pet.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pet < ActiveRecord::Base
2
4
  belongs_to :person, counter_cache: true
3
5
  end
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Relationship < ActiveRecord::Base
2
4
  belongs_to :firm
3
5
  belongs_to :client
data/spec/models/reply.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Reply < ActiveRecord::Base
2
4
  belongs_to :submission
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Student < ActiveRecord::Base
2
4
  has_and_belongs_to_many :teachers
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Submission < ActiveRecord::Base
2
4
  belongs_to :user
3
5
  has_many :replies
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Teacher < ActiveRecord::Base
2
4
  has_and_belongs_to_many :students
3
5
  end
data/spec/models/user.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class User < ActiveRecord::Base
2
4
  has_one :submission
3
5
  belongs_to :category
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Writer < BaseUser
2
4
  end