mongoid-list 0.1.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,844 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::List do
4
+
5
+ describe "Creation" do
6
+
7
+ context "simple" do
8
+
9
+ let!(:list1) { Simple.create }
10
+ let!(:list2) { Simple.create }
11
+
12
+ it "should have set list1's position to 1" do
13
+ list1.position.should eq 1
14
+ end
15
+
16
+ it "should have set list2's position to 2" do
17
+ list2.position.should eq 2
18
+ end
19
+
20
+ end
21
+
22
+ context "in embedded collection" do
23
+
24
+ let!(:container) { Container.create }
25
+ let!(:list1) { container.items.create }
26
+ let!(:list2) { container.items.create }
27
+ let!(:list3) { container.items.create }
28
+ let!(:unrelated_list) { Container.create.items.create }
29
+
30
+ it "should have created the container" do
31
+ container.items.count.should eq 3
32
+ end
33
+
34
+ it "should have set the initial positions for each list item" do
35
+ list1.position.should eq 1
36
+ list2.position.should eq 2
37
+ list3.position.should eq 3
38
+ end
39
+
40
+ it "should have also set the unrelated_list's position independently" do
41
+ unrelated_list.position.should eq 1
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+
49
+ describe "Updating" do
50
+
51
+ let!(:list1) { Simple.create }
52
+ let!(:list2) { Simple.create }
53
+ let!(:list3) { Simple.create }
54
+ let!(:list4) { Simple.create }
55
+
56
+ it "should have set initial positions" do
57
+ list1.position.should eq 1
58
+ list2.position.should eq 2
59
+ list3.position.should eq 3
60
+ list4.position.should eq 4
61
+ end
62
+
63
+ context "for @list2 going to position of 1" do
64
+
65
+ before do
66
+ list2.update_attributes(position: 1)
67
+ list1.reload
68
+ list2.reload
69
+ list3.reload
70
+ list4.reload
71
+ end
72
+
73
+ it "should have updated everyone's :position" do
74
+ list1.position.should eq 2
75
+ list2.position.should eq 1
76
+ list3.position.should eq 3
77
+ list4.position.should eq 4
78
+ end
79
+
80
+ end
81
+
82
+ context "for list3 going to position of 1" do
83
+
84
+ before do
85
+ list3.update_attributes(position: 1)
86
+ list1.reload
87
+ list2.reload
88
+ list3.reload
89
+ list4.reload
90
+ end
91
+
92
+ it "should have updated everyone's :position" do
93
+ list1.position.should eq 2
94
+ list2.position.should eq 3
95
+ list3.position.should eq 1
96
+ list4.position.should eq 4
97
+ end
98
+
99
+ end
100
+
101
+ context "for list1 going to position of 2" do
102
+
103
+ before do
104
+ list1.update_attributes(position: 2)
105
+ list1.reload
106
+ list2.reload
107
+ list3.reload
108
+ list4.reload
109
+ end
110
+
111
+ it "should have updated everyone's :position" do
112
+ list1.position.should eq 2
113
+ list2.position.should eq 1
114
+ list3.position.should eq 3
115
+ list4.position.should eq 4
116
+ end
117
+
118
+ end
119
+
120
+ context "for list1 going to position of 3" do
121
+
122
+ before do
123
+ list1.update_attributes(position: 3)
124
+ list1.reload
125
+ list2.reload
126
+ list3.reload
127
+ list4.reload
128
+ end
129
+
130
+ it "should have updated everyone's :position" do
131
+ list1.position.should eq 3
132
+ list2.position.should eq 1
133
+ list3.position.should eq 2
134
+ list4.position.should eq 4
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+
141
+
142
+ describe "Updating Embedded" do
143
+
144
+ let!(:container) { Container.create }
145
+ let!(:list1) { container.items.create }
146
+ let!(:list2) { container.items.create }
147
+ let!(:list3) { container.items.create }
148
+ let!(:list4) { container.items.create }
149
+
150
+ it "should have set initial positions" do
151
+ list1.position.should eq 1
152
+ list2.position.should eq 2
153
+ list3.position.should eq 3
154
+ list4.position.should eq 4
155
+ end
156
+
157
+ context "for list2 going to position of 1" do
158
+
159
+ before do
160
+ list2.update_attributes(position: 1)
161
+ container.reload
162
+ end
163
+
164
+ it "should have updated everyone's :position" do
165
+ container.items.find(list1.id).position.should eq 2
166
+ container.items.find(list2.id).position.should eq 1
167
+ container.items.find(list3.id).position.should eq 3
168
+ container.items.find(list4.id).position.should eq 4
169
+ end
170
+
171
+ end
172
+
173
+ context "for list3 going to position of 1" do
174
+
175
+ before do
176
+ list3.update_attributes(position: 1)
177
+ container.reload
178
+ end
179
+
180
+ it "should have updated everyone's :position" do
181
+ container.items.find(list1.id).position.should eq 2
182
+ container.items.find(list2.id).position.should eq 3
183
+ container.items.find(list3.id).position.should eq 1
184
+ container.items.find(list4.id).position.should eq 4
185
+ end
186
+
187
+ end
188
+
189
+ context "for list1 going to position of 2" do
190
+
191
+ before do
192
+ list1.update_attributes(position: 2)
193
+ container.reload
194
+ end
195
+
196
+ it "should have updated everyone's :position" do
197
+ container.items.find(list1.id).position.should eq 2
198
+ container.items.find(list2.id).position.should eq 1
199
+ container.items.find(list3.id).position.should eq 3
200
+ container.items.find(list4.id).position.should eq 4
201
+ end
202
+
203
+ end
204
+
205
+ context "for list1 going to position of 3" do
206
+
207
+ before do
208
+ list1.update_attributes(position: 3)
209
+ container.reload
210
+ end
211
+
212
+ it "should have updated everyone's :position" do
213
+ container.items.find(list1.id).position.should eq 3
214
+ container.items.find(list2.id).position.should eq 1
215
+ container.items.find(list3.id).position.should eq 2
216
+ container.items.find(list4.id).position.should eq 4
217
+ end
218
+
219
+ end
220
+
221
+ end
222
+
223
+
224
+ context "#destroy" do
225
+
226
+ let!(:list1) { Simple.create }
227
+ let!(:list2) { Simple.create }
228
+ let!(:list3) { Simple.create }
229
+ let!(:list4) { Simple.create }
230
+
231
+ context "from the first position" do
232
+
233
+ before do
234
+ list1.destroy
235
+ list2.reload
236
+ list3.reload
237
+ list4.reload
238
+ end
239
+
240
+ it "should have moved all other items up" do
241
+ list2.position.should eq 1
242
+ list3.position.should eq 2
243
+ list4.position.should eq 3
244
+ end
245
+
246
+ end
247
+
248
+ context "removing from the 2nd position" do
249
+
250
+ before do
251
+ list2.destroy
252
+ list1.reload
253
+ list3.reload
254
+ list4.reload
255
+ end
256
+
257
+ it "should have kept list1 where it was" do
258
+ list1.position.should eq 1
259
+ end
260
+
261
+ it "should have moved lower items up" do
262
+ list3.position.should eq 2
263
+ list4.position.should eq 3
264
+ end
265
+
266
+ end
267
+
268
+ context "removing from the 4th and last position" do
269
+
270
+ before do
271
+ list4.destroy
272
+ list1.reload
273
+ list2.reload
274
+ list3.reload
275
+ end
276
+
277
+ it "should have kept everything else where it was" do
278
+ list1.position.should eq 1
279
+ list2.position.should eq 2
280
+ list3.position.should eq 3
281
+ end
282
+
283
+ end
284
+
285
+ end
286
+
287
+
288
+ context "#destroy in Embedded" do
289
+
290
+ let!(:container) { Container.create }
291
+ let!(:list1) { container.items.create }
292
+ let!(:list2) { container.items.create }
293
+ let!(:list3) { container.items.create }
294
+ let!(:list4) { container.items.create }
295
+
296
+ context "from the first position" do
297
+
298
+ before do
299
+ list1.destroy
300
+ container.reload
301
+ end
302
+
303
+ it "should have moved all other items up" do
304
+ container.items.find(list2.id).position.should eq 1
305
+ container.items.find(list3.id).position.should eq 2
306
+ container.items.find(list4.id).position.should eq 3
307
+ end
308
+
309
+ end
310
+
311
+ context "removing from the 2nd position" do
312
+
313
+ before do
314
+ list2.destroy
315
+ container.reload
316
+ end
317
+
318
+ it "should have kept list1 where it was" do
319
+ container.items.find(list1.id).position.should eq 1
320
+ end
321
+
322
+ it "should have moved list3 up to :position 2" do
323
+ container.items.find(list3.id).position.should eq 2
324
+ end
325
+
326
+ it "should have moved list4 up to :position 3" do
327
+ container.items.find(list4.id).position.should eq 3
328
+ end
329
+
330
+ end
331
+
332
+ context "removing from the 4th and last position" do
333
+
334
+ before do
335
+ list4.destroy
336
+ container.reload
337
+ end
338
+
339
+ it "should have kept everything else where it was" do
340
+ container.items.find(list1.id).position.should eq 1
341
+ container.items.find(list2.id).position.should eq 2
342
+ container.items.find(list3.id).position.should eq 3
343
+ end
344
+
345
+ end
346
+
347
+ end
348
+
349
+ context "#destroy Item from Deeply Embedded List" do
350
+
351
+ let!(:container) { Container.create }
352
+ let!(:list) { container.items.create }
353
+ let!(:item1) { list.items.create }
354
+ let!(:item2) { list.items.create }
355
+ let!(:item3) { list.items.create }
356
+
357
+ context "from 1st position" do
358
+
359
+ before do
360
+ item1.destroy
361
+ container.reload
362
+ end
363
+
364
+ it "should have moved all other items up" do
365
+ container.items.first.items.find(item2.id).position.should eq 1
366
+ container.items.first.items.find(item3.id).position.should eq 2
367
+ end
368
+
369
+ end
370
+
371
+ context "from middle position" do
372
+
373
+ before do
374
+ item2.destroy
375
+ container.reload
376
+ end
377
+
378
+ it "should have moved last items up" do
379
+ container.items.first.items.find(item1.id).position.should eq 1
380
+ container.items.first.items.find(item3.id).position.should eq 2
381
+ end
382
+
383
+ end
384
+
385
+ end
386
+
387
+
388
+ describe "#list_scoped?" do
389
+
390
+ context "for Simple (unscoped)" do
391
+
392
+ subject { Simple.new }
393
+ it { should_not be_list_scoped }
394
+
395
+ end
396
+
397
+ context "for Scoped" do
398
+
399
+ subject { Scoped.new }
400
+ it { should be_list_scoped }
401
+
402
+ end
403
+
404
+ end
405
+
406
+
407
+ describe "#list_scope_field" do
408
+
409
+ subject { Scoped.create }
410
+
411
+ it "should be :group" do
412
+ subject.list_scope_field.should eq :group
413
+ end
414
+
415
+ end
416
+
417
+
418
+ describe "#list_scope_conditions" do
419
+
420
+ context "when unscoped" do
421
+
422
+ subject { Simple.new }
423
+
424
+ let(:expected_conditions) do
425
+ {}
426
+ end
427
+
428
+ it "should be an empty hash" do
429
+ subject.list_scope_conditions.should eq expected_conditions
430
+ end
431
+
432
+ end
433
+
434
+ context "when scoped" do
435
+
436
+ subject { Scoped.new(group: "a grouping") }
437
+
438
+ let(:expected_conditions) do
439
+ { group: "a grouping" }
440
+ end
441
+
442
+ it "should have be for a :group of 'a grouping'" do
443
+ subject.list_scope_conditions.should eq expected_conditions
444
+ end
445
+
446
+ end
447
+
448
+ end
449
+
450
+
451
+ describe "Initializing in multiple scopes" do
452
+
453
+
454
+ context "on a Collection" do
455
+
456
+ let!(:group1_1) { Scoped.create(group: 1) }
457
+ let!(:group2_1) { Scoped.create(group: 2) }
458
+ let!(:group2_2) { Scoped.create(group: 2) }
459
+
460
+ it "should default group1_1 to a :position of 1" do
461
+ group1_1.position.should eq 1
462
+ end
463
+
464
+ it "should default group2_1 to a :position of 1" do
465
+ group2_1.position.should eq 1
466
+ end
467
+
468
+ it "should default group2_2 to a :position of 2" do
469
+ group2_2.position.should eq 2
470
+ end
471
+ end
472
+
473
+ context "on Embedded Documents" do
474
+
475
+ let!(:container) { Container.create }
476
+ let!(:group1_1) { container.scoped_items.create(group: 1) }
477
+ let!(:group1_2) { container.scoped_items.create(group: 1) }
478
+ let!(:group2_1) { container.scoped_items.create(group: 2) }
479
+ let!(:group2_2) { container.scoped_items.create(group: 2) }
480
+ let!(:group2_3) { container.scoped_items.create(group: 2) }
481
+ let!(:group3_1) { container.scoped_items.create(group: 3) }
482
+
483
+ it "should default group1_1 to a :position of 1" do
484
+ group1_1.position.should eq 1
485
+ end
486
+
487
+ it "should default group1_2 to a :position of 2" do
488
+ group1_2.position.should eq 2
489
+ end
490
+
491
+ it "should default group2_1 to a :position of 1" do
492
+ group2_1.position.should eq 1
493
+ end
494
+
495
+ it "should default group2_2 to a :position of 2" do
496
+ group2_2.position.should eq 2
497
+ end
498
+
499
+ it "should default group2_3 to a :position of 3" do
500
+ group2_3.position.should eq 3
501
+ end
502
+
503
+ it "should default group3_1 to a :position of 1" do
504
+ group3_1.position.should eq 1
505
+ end
506
+
507
+ end
508
+
509
+ end
510
+
511
+
512
+ describe "#update :position" do
513
+
514
+ context "for Collection List" do
515
+
516
+ let!(:group1_1) { Scoped.create(group: 1) }
517
+ let!(:group1_2) { Scoped.create(group: 1) }
518
+ let!(:group2_1) { Scoped.create(group: 2) }
519
+ let!(:group2_2) { Scoped.create(group: 2) }
520
+ let!(:group2_3) { Scoped.create(group: 2) }
521
+
522
+ context "group1_2 to :position 1" do
523
+
524
+ before do
525
+ group1_2.update_attribute(:position, 1)
526
+ end
527
+
528
+ it "should have updated group1_1 to :position of 2" do
529
+ group1_1.position.should eq 1
530
+ group1_1.reload.position.should eq 2
531
+ end
532
+
533
+ it "should not have updated :group2_1's :position" do
534
+ group2_1.reload.position.should eq group2_1.position
535
+ end
536
+
537
+ it "should not have updated :group2_2's :position" do
538
+ group2_2.reload.position.should eq group2_2.position
539
+ end
540
+
541
+ it "should not have updated :group2_3's :position" do
542
+ group2_3.reload.position.should eq group2_3.position
543
+ end
544
+
545
+ end
546
+
547
+ context "group2_2 to :position 3" do
548
+
549
+ before do
550
+ group2_2.update_attribute(:position, 3)
551
+ end
552
+
553
+ it "should not have updated group1_1's :position" do
554
+ group1_1.reload.position.should eq group1_1.position
555
+ end
556
+
557
+ it "should not have updated group1_2's :position" do
558
+ group1_2.reload.position.should eq group1_2.position
559
+ end
560
+
561
+ it "should not have updated group2_1's :position" do
562
+ group2_1.reload.position.should eq group2_1.position
563
+ end
564
+
565
+ it "should have updated group2_2's :position to 3" do
566
+ group2_2.position.should eq 3
567
+ end
568
+
569
+ it "should have updated group2_3's :position to 2" do
570
+ group2_3.position.should eq 3
571
+ group2_3.reload.position.should eq 2
572
+ end
573
+
574
+ end
575
+
576
+ end
577
+
578
+ context "for an Embedded List" do
579
+
580
+ let!(:container) { Container.create }
581
+ let!(:group1_1) { container.scoped_items.create(group: 1) }
582
+ let!(:group1_2) { container.scoped_items.create(group: 1) }
583
+ let!(:group2_1) { container.scoped_items.create(group: 2) }
584
+ let!(:group2_2) { container.scoped_items.create(group: 2) }
585
+ let!(:group2_3) { container.scoped_items.create(group: 2) }
586
+
587
+ context "moving group1_1 to :position 2" do
588
+
589
+ before do
590
+ group1_1.update_attributes(position: 2)
591
+ end
592
+
593
+ it "should update group1_1's :position to 2" do
594
+ group1_1.position.should eq 2
595
+ end
596
+
597
+ it "should update group1_2's :position to 1" do
598
+ group1_2.position.should eq 2
599
+ group1_2.reload.position.should eq 1
600
+ end
601
+
602
+ it "should not update group2_1's :position" do
603
+ group2_1.position.should eq 1
604
+ group2_1.reload.position.should eq 1
605
+ end
606
+
607
+ it "should not update group2_2's :position" do
608
+ group2_2.position.should eq 2
609
+ group2_2.reload.position.should eq 2
610
+ end
611
+
612
+ it "should not update group2_3's :position" do
613
+ group2_3.position.should eq 3
614
+ group2_3.reload.position.should eq 3
615
+ end
616
+
617
+ end
618
+
619
+ context "moving group2_3 to :position 1" do
620
+
621
+ before do
622
+ group2_3.update_attributes(position: 1)
623
+ end
624
+
625
+ it "should not update group1_1's :position" do
626
+ group1_1.position.should eq 1
627
+ group1_1.reload.position.should eq 1
628
+ end
629
+
630
+ it "should not update group1_2's :position" do
631
+ group1_2.position.should eq 2
632
+ group1_2.reload.position.should eq 2
633
+ end
634
+
635
+ it "should update group2_1's :position to 2" do
636
+ group2_1.position.should eq 1
637
+ group2_1.reload.position.should eq 2
638
+ end
639
+
640
+ it "should update group2_2's :position to 3" do
641
+ group2_2.position.should eq 2
642
+ group2_2.reload.position.should eq 3
643
+ end
644
+
645
+ it "should update group2_3's :position to 1" do
646
+ group2_3.position.should eq 1
647
+ group2_3.reload.position.should eq 1
648
+ end
649
+
650
+ end
651
+
652
+ end
653
+
654
+ end
655
+
656
+
657
+ describe "#destroy" do
658
+
659
+ context "from a Collection" do
660
+
661
+ let!(:group1_1) { Scoped.create(group: 1) }
662
+ let!(:group1_2) { Scoped.create(group: 1) }
663
+ let!(:group2_1) { Scoped.create(group: 2) }
664
+ let!(:group2_2) { Scoped.create(group: 2) }
665
+ let!(:group2_3) { Scoped.create(group: 2) }
666
+
667
+ context "for group2_1" do
668
+
669
+ before do
670
+ group2_1.destroy
671
+ end
672
+
673
+ it "should not update group1_1's :position" do
674
+ group1_1.position.should eq 1
675
+ group1_1.reload.position.should eq 1
676
+ end
677
+
678
+ it "should not update group1_2's :position" do
679
+ group1_2.position.should eq 2
680
+ group1_2.reload.position.should eq 2
681
+ end
682
+
683
+ it "should update group2_2's :position to 1" do
684
+ group2_2.position.should eq 2
685
+ group2_2.reload.position.should eq 1
686
+ end
687
+
688
+ it "should update group2_3's :position to 2" do
689
+ group2_3.position.should eq 3
690
+ group2_3.reload.position.should eq 2
691
+ end
692
+
693
+ end
694
+
695
+ end
696
+
697
+ context "from an Embedded Collection" do
698
+
699
+ let!(:container) { Container.create }
700
+ let!(:group1_1) { container.scoped_items.create(group: 1) }
701
+ let!(:group1_2) { container.scoped_items.create(group: 1) }
702
+ let!(:group2_1) { container.scoped_items.create(group: 2) }
703
+ let!(:group2_2) { container.scoped_items.create(group: 2) }
704
+ let!(:group2_3) { container.scoped_items.create(group: 2) }
705
+
706
+ context "for group2_2" do
707
+
708
+ before do
709
+ group2_2.destroy
710
+ end
711
+
712
+ it "should not update group1_1's :position" do
713
+ group1_1.position.should eq 1
714
+ group1_1.reload.position.should eq 1
715
+ end
716
+
717
+ it "should not update group1_2's :position" do
718
+ group1_2.position.should eq 2
719
+ group1_2.reload.position.should eq 2
720
+ end
721
+
722
+ it "should not update group2_1's :position" do
723
+ group2_1.position.should eq 1
724
+ group2_1.reload.position.should eq 1
725
+ end
726
+
727
+ it "should update group2_3's :position to 2" do
728
+ group2_3.position.should eq 3
729
+ container.reload.scoped_items.find(group2_3.id).position.should eq 2
730
+ end
731
+
732
+ end
733
+
734
+ end
735
+
736
+ end
737
+
738
+
739
+ describe "#update_positions_in_list!" do
740
+
741
+ context "on a Collection" do
742
+
743
+ let!(:obj1) { Simple.create }
744
+ let!(:obj2) { Simple.create }
745
+ let!(:obj3) { Simple.create }
746
+
747
+ before do
748
+ Simple.update_positions_in_list!([obj2.id, obj1.id, obj3.id])
749
+ end
750
+
751
+ it "should change obj1 from :position of 1 to 2" do
752
+ obj1.position.should eq 1
753
+ obj1.reload.position.should eq 2
754
+ end
755
+
756
+ it "should change obj2 from :position of 2 to 1" do
757
+ obj2.position.should eq 2
758
+ obj2.reload.position.should eq 1
759
+ end
760
+
761
+ it "should not change obj3 from :position of 3" do
762
+ obj3.position.should eq 3
763
+ obj3.reload.position.should eq 3
764
+ end
765
+
766
+ end
767
+
768
+ context "on an Embedded Collection" do
769
+
770
+ let(:container) { Container.create! }
771
+ let!(:obj1) { container.items.create! }
772
+ let!(:obj2) { container.items.create! }
773
+ let!(:obj3) { container.items.create! }
774
+ let!(:obj4) { container.items.create! }
775
+
776
+ before do
777
+ container.items.update_positions_in_list!([obj2.id, obj1.id, obj4.id, obj3.id])
778
+ end
779
+
780
+ it "should change obj1 from :position of 1 to 2" do
781
+ obj1.position.should eq 1
782
+ obj1.reload.position.should eq 2
783
+ end
784
+
785
+ it "should change obj2 from :position of 2 to 1" do
786
+ obj2.position.should eq 2
787
+ obj2.reload.position.should eq 1
788
+ end
789
+
790
+ it "should change obj3 from :position of 3 to 4" do
791
+ obj3.position.should eq 3
792
+ obj3.reload.position.should eq 4
793
+ end
794
+
795
+ it "should change obj4 from :position of 4 to 3" do
796
+ obj4.position.should eq 4
797
+ obj4.reload.position.should eq 3
798
+ end
799
+ end
800
+
801
+ context "on a Deeply Embedded Collection" do
802
+
803
+ let(:root) do
804
+ Container.create!
805
+ end
806
+
807
+ let(:embedded) do
808
+ root.items.create!
809
+ end
810
+
811
+ let!(:obj1) { embedded.items.create! }
812
+ let!(:obj2) { embedded.items.create! }
813
+ let!(:obj3) { embedded.items.create! }
814
+ let!(:obj4) { embedded.items.create! }
815
+
816
+ before do
817
+ embedded.items.update_positions_in_list!([obj3.id, obj4.id, obj1.id, obj2.id])
818
+ end
819
+
820
+ it "should change obj1 from :position of 1 to 3" do
821
+ obj1.position.should eq 1
822
+ obj1.reload.position.should eq 3
823
+ end
824
+
825
+ it "should change @obj2 from :position of 2 to 4" do
826
+ obj2.position.should eq 2
827
+ obj2.reload.position.should eq 4
828
+ end
829
+
830
+ it "should change @obj3 from :position of 3 to 1" do
831
+ obj3.position.should eq 3
832
+ obj3.reload.position.should eq 1
833
+ end
834
+
835
+ it "should change @obj4 from :position of 4 to 2" do
836
+ obj4.position.should eq 4
837
+ obj4.reload.position.should eq 2
838
+ end
839
+
840
+ end
841
+
842
+ end
843
+
844
+ end