active_mongoid 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +22 -0
- data/.ruby_gemset +1 -0
- data/.ruby_version +1 -0
- data/.travis.yml +11 -0
- data/Appraisals +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +198 -0
- data/Rakefile +7 -0
- data/active_mongoid.gemspec +36 -0
- data/gemfiles/mongoid_2.8.gemfile +7 -0
- data/gemfiles/mongoid_2.8.gemfile.lock +105 -0
- data/gemfiles/mongoid_3.1.gemfile +7 -0
- data/gemfiles/mongoid_3.1.gemfile.lock +108 -0
- data/lib/active_mongoid.rb +8 -0
- data/lib/active_mongoid/associations.rb +82 -0
- data/lib/active_mongoid/associations/binding.rb +77 -0
- data/lib/active_mongoid/associations/builder.rb +26 -0
- data/lib/active_mongoid/associations/builders/in.rb +17 -0
- data/lib/active_mongoid/associations/builders/many.rb +15 -0
- data/lib/active_mongoid/associations/builders/one.rb +15 -0
- data/lib/active_mongoid/associations/document_relation/accessors.rb +100 -0
- data/lib/active_mongoid/associations/document_relation/associations.rb +33 -0
- data/lib/active_mongoid/associations/document_relation/auto_save.rb +31 -0
- data/lib/active_mongoid/associations/document_relation/bindings/in.rb +48 -0
- data/lib/active_mongoid/associations/document_relation/bindings/many.rb +19 -0
- data/lib/active_mongoid/associations/document_relation/bindings/one.rb +19 -0
- data/lib/active_mongoid/associations/document_relation/builders.rb +31 -0
- data/lib/active_mongoid/associations/document_relation/dependent.rb +26 -0
- data/lib/active_mongoid/associations/document_relation/macros.rb +51 -0
- data/lib/active_mongoid/associations/document_relation/referenced/in.rb +72 -0
- data/lib/active_mongoid/associations/document_relation/referenced/many.rb +125 -0
- data/lib/active_mongoid/associations/document_relation/referenced/one.rb +75 -0
- data/lib/active_mongoid/associations/many.rb +211 -0
- data/lib/active_mongoid/associations/metadata.rb +229 -0
- data/lib/active_mongoid/associations/one.rb +21 -0
- data/lib/active_mongoid/associations/proxy.rb +38 -0
- data/lib/active_mongoid/associations/record_relation/accessors.rb +80 -0
- data/lib/active_mongoid/associations/record_relation/associations.rb +33 -0
- data/lib/active_mongoid/associations/record_relation/auto_save.rb +43 -0
- data/lib/active_mongoid/associations/record_relation/bindings/in.rb +48 -0
- data/lib/active_mongoid/associations/record_relation/bindings/many.rb +19 -0
- data/lib/active_mongoid/associations/record_relation/bindings/one.rb +19 -0
- data/lib/active_mongoid/associations/record_relation/builders.rb +31 -0
- data/lib/active_mongoid/associations/record_relation/dependent.rb +26 -0
- data/lib/active_mongoid/associations/record_relation/macros.rb +65 -0
- data/lib/active_mongoid/associations/record_relation/referenced/in.rb +72 -0
- data/lib/active_mongoid/associations/record_relation/referenced/many.rb +128 -0
- data/lib/active_mongoid/associations/record_relation/referenced/one.rb +75 -0
- data/lib/active_mongoid/associations/targets/enumerable.rb +161 -0
- data/lib/active_mongoid/bson_id.rb +44 -0
- data/lib/active_mongoid/finder_proxy.rb +55 -0
- data/lib/active_mongoid/finders.rb +60 -0
- data/lib/active_mongoid/version.rb +3 -0
- data/spec/lib/associations/document_relation/accessors_spec.rb +330 -0
- data/spec/lib/associations/document_relation/auto_save_spec.rb +157 -0
- data/spec/lib/associations/document_relation/bindings/in_spec.rb +39 -0
- data/spec/lib/associations/document_relation/bindings/many_spec.rb +36 -0
- data/spec/lib/associations/document_relation/bindings/one_spec.rb +39 -0
- data/spec/lib/associations/document_relation/builders_spec.rb +117 -0
- data/spec/lib/associations/document_relation/dependent_spec.rb +87 -0
- data/spec/lib/associations/document_relation/macros_spec.rb +68 -0
- data/spec/lib/associations/document_relation/referenced/in_spec.rb +27 -0
- data/spec/lib/associations/document_relation/referenced/many_spec.rb +32 -0
- data/spec/lib/associations/document_relation/referenced/one_spec.rb +28 -0
- data/spec/lib/associations/metadata_spec.rb +157 -0
- data/spec/lib/associations/record_relation/accessors_spec.rb +328 -0
- data/spec/lib/associations/record_relation/auto_save_spec.rb +157 -0
- data/spec/lib/associations/record_relation/bindings/in_spec.rb +39 -0
- data/spec/lib/associations/record_relation/bindings/many_spec.rb +39 -0
- data/spec/lib/associations/record_relation/bindings/one_spec.rb +57 -0
- data/spec/lib/associations/record_relation/builders_spec.rb +118 -0
- data/spec/lib/associations/record_relation/dependent_spec.rb +87 -0
- data/spec/lib/associations/record_relation/macros_spec.rb +73 -0
- data/spec/lib/associations/record_relation/referenced/in_spec.rb +27 -0
- data/spec/lib/associations/record_relation/referenced/many_spec.rb +32 -0
- data/spec/lib/associations/record_relation/referenced/one_spec.rb +27 -0
- data/spec/lib/bson_id_spec.rb +48 -0
- data/spec/lib/finders_spec.rb +105 -0
- data/spec/spec_helper.rb +89 -0
- data/spec/support/models/active_record/address.rb +6 -0
- data/spec/support/models/active_record/division.rb +16 -0
- data/spec/support/models/active_record/division_setting.rb +9 -0
- data/spec/support/models/active_record/player.rb +12 -0
- data/spec/support/models/mongoid/league.rb +10 -0
- data/spec/support/models/mongoid/person.rb +9 -0
- data/spec/support/models/mongoid/post.rb +9 -0
- data/spec/support/models/mongoid/stat.rb +8 -0
- data/spec/support/models/mongoid/team.rb +9 -0
- data/spec/support/shared_examples/shared_many_spec.rb +411 -0
- metadata +370 -0
@@ -0,0 +1,411 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
shared_examples "a has_many relation" do
|
4
|
+
|
5
|
+
let(:target) { target_class.new }
|
6
|
+
let(:target_1) { target_class.new(name: "1") }
|
7
|
+
let(:target_2) { target_class.new(name: "2") }
|
8
|
+
let(:targets) { [target_1, target_2] }
|
9
|
+
let(:new_base) { base_class.new }
|
10
|
+
let(:persisted_base) { base_class.create }
|
11
|
+
let(:new_relation) { new_base.send(relation_name) }
|
12
|
+
let(:persisted_relation) { persisted_base.send(relation_name) }
|
13
|
+
let(:metadata) { base_class.am_relations[relation_name.to_s] }
|
14
|
+
|
15
|
+
describe ".<<" do
|
16
|
+
|
17
|
+
context "when base is not persisted" do
|
18
|
+
|
19
|
+
before do
|
20
|
+
new_relation << target
|
21
|
+
end
|
22
|
+
|
23
|
+
after do
|
24
|
+
new_relation.clear
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when target is empty" do
|
28
|
+
|
29
|
+
it "initializes with enumerable target" do
|
30
|
+
expect(new_relation.size).to eq(1)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "does not persist" do
|
34
|
+
new_relation.each do |target|
|
35
|
+
expect(target).to_not be_persisted
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when target is not empty" do
|
42
|
+
|
43
|
+
let(:new_target) do
|
44
|
+
target_class.new
|
45
|
+
end
|
46
|
+
|
47
|
+
before do
|
48
|
+
new_relation << new_target
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
it "append with enumerable target" do
|
53
|
+
expect(new_relation.size).to eq(2)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "does not persist" do
|
57
|
+
new_relation.each do |target|
|
58
|
+
expect(target).to_not be_persisted
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
context "when base is persisted" do
|
67
|
+
|
68
|
+
before do
|
69
|
+
persisted_relation << target
|
70
|
+
end
|
71
|
+
|
72
|
+
after do
|
73
|
+
persisted_relation.clear
|
74
|
+
end
|
75
|
+
|
76
|
+
context "when target is empty" do
|
77
|
+
|
78
|
+
it "initializes with enumerable target" do
|
79
|
+
expect(persisted_relation.size).to eq(1)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "does persist" do
|
83
|
+
persisted_relation.each do |target|
|
84
|
+
expect(target).to be_persisted
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
it "binds foreign_key" do
|
89
|
+
persisted_relation.each do |target|
|
90
|
+
expect(target.send(metadata.foreign_key)).to eq(persisted_base.id)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
context "when target is not empty" do
|
97
|
+
|
98
|
+
let(:new_target) do
|
99
|
+
target_class.new
|
100
|
+
end
|
101
|
+
|
102
|
+
before do
|
103
|
+
persisted_relation << new_target
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
it "append with enumerable target" do
|
108
|
+
expect(persisted_relation.size).to eq(2)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "does persist" do
|
112
|
+
persisted_relation.each do |target|
|
113
|
+
expect(target).to be_persisted
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
it "binds foreign_key" do
|
118
|
+
persisted_relation.each do |target|
|
119
|
+
expect(target.send(metadata.foreign_key)).to eq(persisted_base.id)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
describe ".build" do
|
131
|
+
|
132
|
+
context "when base is not persisted" do
|
133
|
+
|
134
|
+
before do
|
135
|
+
new_relation.build
|
136
|
+
end
|
137
|
+
|
138
|
+
after do
|
139
|
+
new_relation.clear
|
140
|
+
end
|
141
|
+
|
142
|
+
context "when target is empty" do
|
143
|
+
|
144
|
+
it "initializes with enumerable target" do
|
145
|
+
expect(new_relation.size).to eq(1)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "does not persist" do
|
149
|
+
new_relation.each do |taret|
|
150
|
+
expect(target).to_not be_persisted
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
context "when target is not empty" do
|
157
|
+
|
158
|
+
before do
|
159
|
+
new_relation.build
|
160
|
+
end
|
161
|
+
|
162
|
+
it "append with enumerable target" do
|
163
|
+
expect(new_relation.size).to eq(2)
|
164
|
+
end
|
165
|
+
|
166
|
+
it "does not persist" do
|
167
|
+
new_relation.each do |target|
|
168
|
+
expect(target).to_not be_persisted
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
175
|
+
|
176
|
+
context "when base is persisted" do
|
177
|
+
|
178
|
+
before do
|
179
|
+
persisted_relation.build
|
180
|
+
end
|
181
|
+
|
182
|
+
after do
|
183
|
+
persisted_relation.clear
|
184
|
+
end
|
185
|
+
|
186
|
+
context "when target is empty" do
|
187
|
+
|
188
|
+
it "initializes with enumerable target" do
|
189
|
+
expect(persisted_relation.size).to eq(1)
|
190
|
+
end
|
191
|
+
|
192
|
+
it "does not persist" do
|
193
|
+
persisted_relation.each do |target|
|
194
|
+
expect(target).to_not be_persisted
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
it "binds foreign_key" do
|
199
|
+
persisted_relation.each do |target|
|
200
|
+
expect(target.send(metadata.foreign_key)).to eq(persisted_base.id)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
end
|
205
|
+
|
206
|
+
context "when target is not empty" do
|
207
|
+
|
208
|
+
before do
|
209
|
+
persisted_relation.build
|
210
|
+
end
|
211
|
+
|
212
|
+
it "append with enumerable target" do
|
213
|
+
expect(persisted_relation.size).to eq(2)
|
214
|
+
end
|
215
|
+
|
216
|
+
it "does not persist" do
|
217
|
+
persisted_relation.each do |target|
|
218
|
+
expect(target).to_not be_persisted
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
it "binds foreign_key" do
|
223
|
+
persisted_relation.each do |target|
|
224
|
+
expect(target.send(metadata.foreign_key)).to eq(persisted_base.id)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
end
|
229
|
+
|
230
|
+
end
|
231
|
+
|
232
|
+
end
|
233
|
+
|
234
|
+
[ :create, :create! ].each do |method|
|
235
|
+
|
236
|
+
describe ".#{method}" do
|
237
|
+
|
238
|
+
context "when base is persisted" do
|
239
|
+
|
240
|
+
before do
|
241
|
+
persisted_relation.send(method)
|
242
|
+
end
|
243
|
+
|
244
|
+
after do
|
245
|
+
persisted_relation.clear
|
246
|
+
end
|
247
|
+
|
248
|
+
context "when target is empty" do
|
249
|
+
|
250
|
+
it "initializes with enumerable target" do
|
251
|
+
expect(persisted_relation.size).to eq(1)
|
252
|
+
end
|
253
|
+
|
254
|
+
it "does not persist" do
|
255
|
+
persisted_relation.each do |target|
|
256
|
+
expect(target).to be_persisted
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
end
|
261
|
+
|
262
|
+
context "when target is not empty" do
|
263
|
+
|
264
|
+
context "when sending single" do
|
265
|
+
|
266
|
+
before do
|
267
|
+
persisted_relation.send(method)
|
268
|
+
end
|
269
|
+
|
270
|
+
it "append with enumerable target" do
|
271
|
+
expect(persisted_relation.size).to eq(2)
|
272
|
+
end
|
273
|
+
|
274
|
+
it "does not persist" do
|
275
|
+
persisted_relation.each do |target|
|
276
|
+
expect(target).to be_persisted
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
end
|
281
|
+
|
282
|
+
context "when sending multiple" do
|
283
|
+
|
284
|
+
before do
|
285
|
+
persisted_relation.send(method, [{name: 1}, {name: 2}])
|
286
|
+
end
|
287
|
+
|
288
|
+
it "append with enumerable target" do
|
289
|
+
expect(persisted_relation.size).to eq(3)
|
290
|
+
end
|
291
|
+
|
292
|
+
it "does not persist" do
|
293
|
+
persisted_relation.each do |target|
|
294
|
+
expect(target).to be_persisted
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
end
|
302
|
+
|
303
|
+
end
|
304
|
+
|
305
|
+
end
|
306
|
+
|
307
|
+
context ".delete" do
|
308
|
+
|
309
|
+
before do
|
310
|
+
persisted_relation << targets
|
311
|
+
end
|
312
|
+
|
313
|
+
it "deletes the specified target" do
|
314
|
+
expect(target_1.id).to_not be_nil
|
315
|
+
expect(persisted_relation.delete(target_1)).to eq(target_1)
|
316
|
+
expect(target_1.send(metadata.foreign_key)).to be_nil
|
317
|
+
expect(persisted_relation.first.id).to eq(target_2.id)
|
318
|
+
end
|
319
|
+
|
320
|
+
it "deletes all targets" do
|
321
|
+
id = persisted_base.id
|
322
|
+
id = id.is_a?(::ActiveMongoid::BSON::ObjectId) ? id.to_s : id
|
323
|
+
expect(target_class).to receive(:delete_all).once
|
324
|
+
persisted_relation.delete_all
|
325
|
+
end
|
326
|
+
|
327
|
+
it "destroys all targets" do
|
328
|
+
id = persisted_base.id
|
329
|
+
id = id.is_a?(::ActiveMongoid::BSON::ObjectId) ? id.to_s : id
|
330
|
+
expect(target_class).to receive(:destroy_all).once.with({metadata.foreign_key.to_s => id})
|
331
|
+
persisted_relation.destroy_all
|
332
|
+
end
|
333
|
+
|
334
|
+
end
|
335
|
+
|
336
|
+
context ".remove_not_in" do
|
337
|
+
|
338
|
+
before do
|
339
|
+
persisted_relation << targets
|
340
|
+
end
|
341
|
+
|
342
|
+
it "removes all except for the specified target" do
|
343
|
+
persisted_relation.send(:remove_not_in, [target_1.id])
|
344
|
+
expect(persisted_relation).to eq([target_1])
|
345
|
+
end
|
346
|
+
|
347
|
+
end
|
348
|
+
|
349
|
+
context ".find" do
|
350
|
+
|
351
|
+
before do
|
352
|
+
persisted_relation << targets
|
353
|
+
end
|
354
|
+
|
355
|
+
after do
|
356
|
+
persisted_relation.clear
|
357
|
+
end
|
358
|
+
|
359
|
+
it "finds the specified target" do
|
360
|
+
target = persisted_relation.find(target_1.id)
|
361
|
+
expect(target).to eq(target_1)
|
362
|
+
end
|
363
|
+
|
364
|
+
context "when find_or" do
|
365
|
+
|
366
|
+
context "when initialize_by" do
|
367
|
+
|
368
|
+
let(:new_name) do
|
369
|
+
"foo"
|
370
|
+
end
|
371
|
+
|
372
|
+
it "returns item when found" do
|
373
|
+
target = persisted_relation.find_or_initialize_by({name: "1"})
|
374
|
+
expect(target).to eq(target_1)
|
375
|
+
expect(target).to be_persisted
|
376
|
+
end
|
377
|
+
|
378
|
+
it "initializes item when not found" do
|
379
|
+
target = persisted_relation.find_or_initialize_by({name: new_name})
|
380
|
+
expect(target.name).to eq(new_name)
|
381
|
+
expect(target).to_not be_persisted
|
382
|
+
end
|
383
|
+
|
384
|
+
end
|
385
|
+
|
386
|
+
context "when create_by" do
|
387
|
+
|
388
|
+
let(:new_name) do
|
389
|
+
"foo"
|
390
|
+
end
|
391
|
+
|
392
|
+
it "returns item when found" do
|
393
|
+
target = persisted_relation.find_or_create_by({name: "1"})
|
394
|
+
expect(target).to eq(target_1)
|
395
|
+
expect(target).to be_persisted
|
396
|
+
end
|
397
|
+
|
398
|
+
it "initializes item when not found" do
|
399
|
+
target = persisted_relation.find_or_create_by({name: new_name})
|
400
|
+
expect(target.name).to eq(new_name)
|
401
|
+
expect(target).to be_persisted
|
402
|
+
end
|
403
|
+
|
404
|
+
end
|
405
|
+
|
406
|
+
end
|
407
|
+
|
408
|
+
end
|
409
|
+
|
410
|
+
|
411
|
+
end
|