-
Notifications
You must be signed in to change notification settings - Fork 20
/
mongoid_alize_spec.rb
552 lines (475 loc) · 14 KB
/
mongoid_alize_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
require 'spec_helper'
describe Mongoid::Alize do
def person_fields
[:name, :location]
end
def head_fields
[:size]
end
before do
@now = Time.now
@head = Head.new(:size => @size = 10, created_at: @now)
@person = Person.new(:name => @name = "Bob", created_at: @now)
end
describe "one-to-one" do
describe "from belongs_to side" do
before do
Head.send(:alize, :person, *person_fields)
@head.person = @person
end
def assert_head
@head.person_fields.should == {
"name" => @name,
"location" => "Paris"
}
end
it "should pull data from person on create" do
@head.save!
assert_head
end
it "should pull data from a changed person on save" do
@head.save!
@head.person = Person.create(:name => @name = "Bill")
@head.save!
assert_head
end
it "should not pull data from an unchanged person on save" do
@head.save!
@head.person_fields["name"] = "Cowboy"
@head.save!
@head.person_fields["name"].should == "Cowboy"
end
it "should push data to head" do
@person.update_attributes!(:name => @name = "Bill")
assert_head
end
it "should nillify person fields in head when person is destroyed" do
@head.update_attributes!(:person_fields => { "name" => "Old Gregg", "location" => "Paris" })
@person.destroy
@head.person_fields.should be_nil
end
end
describe "from has_one side" do
before do
Person.send(:alize, :head, *head_fields)
@person.head = @head
end
def assert_person
@person.head_fields.should == { "size" => @size }
end
it "should pull data from head on create" do
@person.save!
assert_person
end
it "should pull data from head on save" do
@person.save!
@person.head = Head.create(:size => @size = 18)
@person.save!
assert_person
end
it "should push data to person" do
@head.update_attributes!(:size => @size = 20)
assert_person
end
it "should nillify head fields in person when head is destroyed" do
@person.update_attributes!(:head_fields => { "size" => "1000 balloons"})
@head.destroy
@person.head_fields.should be_nil
end
end
end
describe "one-to-many" do
describe "from belongs_to side" do
before do
Head.send(:alize, :captor, *person_fields)
@head.captor = @person
end
def assert_captor
@head.captor_fields.should == { "name" => @name, "location" => "Paris" }
end
it "should pull data from captor on create" do
@head.save!
assert_captor
end
it "should pull data from captor on save" do
@head.save!
@head.captor = Person.create(:name => @name = "Bill")
@head.save!
assert_captor
end
it "should push data to person" do
@person.update_attributes!(:name => @name = "Bill")
assert_captor
end
it "should nillify captor fields when person is destroyed" do
@head.update_attributes!(:captor => { "name" => "Old Gregg"})
@person.destroy
@head.captor_fields.should be_nil
end
end
describe "from has_many side" do
before do
Head.send(:alize, :sees, *person_fields)
@head.sees = [@person]
end
def assert_sees
@head.sees_fields.should == [{
"_id" => @person.id,
"location" => "Paris",
"name" => @name }]
end
it "should pull data from sees on create" do
@head.save!
assert_sees
end
it "should pull data from a sees on save" do
@head.save!
@head.sees = [@person = Person.create(:name => @name = "Bill")]
@head.save!
assert_sees
end
it "should push data to seen_by" do
@person.update_attributes!(:name => @name = "Bill")
assert_sees
end
it "should remove sees_fields entries in head when person is destroyed" do
@head.save!
assert_sees
@person.destroy
@head.sees_fields.should == []
@head.reload.sees_fields.should == []
end
end
end
describe "many-to-many" do
describe "has_and_belongs_to_many" do
before do
Head.send(:alize, :wanted_by, *person_fields)
end
def assert_wanted_by
@head.wanted_by_fields.should == [{
"_id" => @person.id,
"location" => "Paris",
"name" => @name }]
end
it "should pull data from wanted_by on create" do
@head.wanted_by = [@person]
@head.save!
assert_wanted_by
end
it "should pull data from wanted_by on save" do
@head.save!
@person = Person.create(:name => @name = "Bill")
@head.wanted_by = [@person]
@head.save!
assert_wanted_by
end
it "should push data to wants" do
@person.wants = [@head]
@person.update_attributes!(:name => @name = "Bill")
assert_wanted_by
end
it "should remove wanted_by_fields entries in head when person is destroyed" do
@head.wanted_by = [@person]
@head.save!
assert_wanted_by
@person.destroy
@head.reload
@head.wanted_by_fields.should == []
@head.reload.wanted_by_fields.should == []
end
end
end
describe "without specifying fields" do
describe "for non-polymorphic" do
before do
@head.person = @person
end
it "should denormalize all non-internal fields" do
Head.send(:alize, :person)
@head.save!
if Mongoid::Compatibility::Version.mongoid3?
@head.person_fields.should == {
"name" => @name,
"created_at" => @person.created_at,
"seen_by_id" => nil,
"want_ids" => [],
"above_id" => nil,
"above_field" => nil,
"above_type" => nil
}
else
@head.person_fields.should == {
"name" => @name,
"created_at" => @person.created_at,
"my_date" => nil,
"my_datetime" => nil,
"seen_by_id" => nil,
"want_ids" => [],
"above_id" => nil,
"above_type" => nil
}
end
end
it "should denormalize all non-internal fields" do
Head.send(:alize, :person)
@person.save!
if Mongoid::Compatibility::Version.mongoid3?
@head.person_fields.should == {
"name" => @name,
"created_at" => @person.created_at,
"seen_by_id" => nil,
"want_ids" => [],
"above_id" => nil,
"above_field" => nil,
"above_type" => nil
}
else
@head.person_fields.should == {
"name" => @name,
"created_at" => @person.created_at,
"my_date" => nil,
"my_datetime" => nil,
"seen_by_id" => nil,
"want_ids" => [],
"above_id" => nil,
"above_type" => nil
}
end
end
end
describe "for polymorphic" do
before do
@head.person = @person
end
it "should denormalize no default fields for a polymorphic child side" do
@head.nearest = @person
Head.send(:alize_from, :nearest)
@head.save!
@head.nearest_fields.should == {}
end
end
end
describe "overriding denormalize methods for custom behavior on the from side" do
before do
class Person
def denormalize_update_name_first
self.name = "Overrider"
end
end
class Head
def denormalize_from_person
self.person.denormalize_update_name_first
_denormalize_from_person
end
end
Head.send(:alize, :person, *person_fields)
@head.person = @person
end
it "should be possible to define a method before alize and call the alize version within it" do
@head.save!
@head.person_fields["name"].should == "Overrider"
end
end
describe "overriding denormalize methods for custom behavior on the to side" do
before do
class Person
def denormalize_to_head
self.name = "Overrider"
_denormalize_to_head
end
end
Head.send(:alize, :person, *person_fields)
@head.person = @person
@head.save!
end
it "should be possible to define a method before alize and call the alize version within it" do
@person.update_attributes!(:name => @name = "Bill")
@head.person_fields["name"].should == "Overrider"
end
end
describe "forcing denormalization" do
before do
Head.send(:alize, :person, *person_fields)
@head.person = @person
@head.save!
end
it "should allow using the force flag to force denormalization" do
class Head
def denormalize_from_person
_denormalize_from_person(true)
end
end
@head.person_fields["name"] = "Misty"
@head.save!
@head.person_fields["name"] = @name
end
it "should allow using the force_denormalization attribute to force denormalization" do
@head.person_fields["name"] = "Misty"
@head.force_denormalization = true
@head.save!
@head.person_fields["name"] = @name
end
end
describe "using a proc to define fields" do
before do
Head.send(:alize, :person, :fields => lambda { |inverse|
self.alize_fields(inverse) })
@head.person = @person
end
def assert_head
@head.person_fields.should == {
"name" => @name,
"location" => "Paris"
}
end
it "should work the same way as it does with fields specified" do
@head.save!
assert_head
end
end
describe "using a proc to define fields for a one-to-one polymorphic association from the belongs to side" do
before do
Head.send(:alize, :nearest, :fields => lambda { |inverse|
self.alize_fields(inverse) })
@head.nearest = @person
end
def assert_head
@head.nearest_fields.should == {
"name" => @name,
"location" => "Paris"
}
end
it "should work the same way as it does with fields specified" do
@head.save!
assert_head
end
end
describe "using a proc to define fields for a one-to-one polymorphic association from the has one side" do
before do
Person.send(:alize, :nearest_head, :fields => lambda { |inverse|
self.alize_fields(inverse) })
@person.nearest_head = @head
end
def assert_person
@person.nearest_head_fields.should == {
"size" => @size
}
end
it "should work the same way as it does with fields specified" do
@person.save!
assert_person
end
end
describe "using a proc to define fields for a has many polymorphic association from the :as side" do
before do
Head.send(:alize, :below_people, :fields => lambda { |inverse|
self.alize_fields(inverse) })
@head.below_people << @person
end
def assert_head
@head.below_people_fields.should == [{
"_id" => @person.id,
"name" => @name,
"location" => "Paris"
}]
end
it "should work the same way as it does with fields specified" do
@head.save!
assert_head
end
end
describe "using a proc to define fields for a has many polymorphic association from the belongs_to side" do
before do
Person.send(:alize, :above, :fields => lambda { |inverse|
self.alize_fields(inverse) })
@person.above = @head
end
def assert_person
@person.above_fields.should == {
"size" => @size,
}
end
it "should work the same way as it does with fields specified" do
@person.save!
assert_person
end
end
describe "the push on the child side of a one-to-one polymorphic" do
before do
fields = { :fields => lambda { |person| [:name, :location] } }
Head.send(:alize, :nearest, fields)
Person.send(:alize_to, :nearest_head, fields)
@head.nearest = @person
end
def assert_head
@head.nearest_fields.should == {
"name" => @name,
"location" => "Paris"
}
end
it "should push the new fields" do
@head.save!
assert_head
@person.update_attributes!(:name => @name = "George")
assert_head
end
end
describe "the push on the child side of a one-to-many polymorphic" do
before do
fields = { :fields => lambda { |head| [:size] } }
Person.send(:alize, :above, fields)
Head.send(:alize_to, :below_people, fields)
@person.above = @head
end
def assert_person
@person.above_fields.should == {
"size" => @size
}
end
it "should push the new fields" do
@person.save!
assert_person
@head.update_attributes!(:size => @size = 5)
assert_person
end
end
describe "the push on the parent side of a one-to-one polymorphic" do
before do
fields = { :fields => lambda { |inverse| [:size] } }
Person.send(:alize, :nearest_head, fields)
@person.nearest_head = @head
@person.save!
end
def assert_person
@person.nearest_head_fields.should == {
"size" => @size
}
end
it "should push the new fields" do
assert_person
@head.update_attributes!(:size => @size = 5)
assert_person
end
end
describe "the push on the parent side of a one-to-many polymorphic" do
before do
fields = { :fields => lambda { |inverse| [:name, :location] } }
Head.send(:alize, :below_people, fields)
@head.below_people << @person
@head.save!
end
def assert_head
@head.below_people_fields.should == [{
"_id" => @person.id,
"name" => @name,
"location" => "Paris"
}]
end
it "should push the new fields" do
assert_head
@person.update_attributes!(:name => @name = "George")
assert_head
end
end
end