-
Notifications
You must be signed in to change notification settings - Fork 5
/
sortifiable_test.rb
799 lines (587 loc) · 24.4 KB
/
sortifiable_test.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
require 'test_helper'
class NonListTest < ActiveSupport::TestCase
def test_callbacks_are_not_added_to_all_models
Mixin.create! :pos => 1, :parent_id => 5
assert_equal 1, Mixin.first.id
Mixin.find(1).destroy
assert_equal [], Mixin.all
end
def test_instance_methods_are_not_included_in_all_models
Mixin.create! :pos => 1, :parent_id => 5
assert_equal false, Mixin.first.respond_to?(:in_list?)
end
end
class ListTest < ActiveSupport::TestCase
def setup
[5, 6].each do |parent_id|
(1..4).each do |i|
ListMixin.create! :pos => i, :parent_id => parent_id
end
end
end
def test_reordering
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
ListMixin.find(2).move_lower
assert_equal [1, 3, 2, 4], ListMixin.where(:parent_id => 5).map(&:id)
ListMixin.find(2).move_higher
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
ListMixin.find(1).move_to_bottom
assert_equal [2, 3, 4, 1], ListMixin.where(:parent_id => 5).map(&:id)
ListMixin.find(1).move_to_top
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
ListMixin.find(2).move_to_bottom
assert_equal [1, 3, 4, 2], ListMixin.where(:parent_id => 5).map(&:id)
ListMixin.find(4).move_to_top
assert_equal [4, 1, 3, 2], ListMixin.where(:parent_id => 5).map(&:id)
end
def test_bounds_checking
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:pos)
ListMixin.find(1).move_higher
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:pos)
ListMixin.find(4).move_lower
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:pos)
end
def test_move_to_bottom_with_next_to_last_item
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
ListMixin.find(3).move_to_bottom
assert_equal [1, 2, 4, 3], ListMixin.where(:parent_id => 5).map(&:id)
end
def test_next_prev
assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
assert_nil ListMixin.find(1).higher_item
assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
assert_nil ListMixin.find(4).lower_item
end
def test_injection
item = ListMixin.new(:parent_id => 1)
assert_equal({ :parent_id => 1 }, item.send(:scope_condition))
assert_equal("pos", item.send(:position_column))
end
def test_insert
new = ListMixin.create(:parent_id => 20)
assert_equal 1, new.pos
assert new.first?
assert new.last?
new = ListMixin.create(:parent_id => 20)
assert_equal 2, new.pos
assert !new.first?
assert new.last?
new = ListMixin.create(:parent_id => 20)
assert_equal 3, new.pos
assert !new.first?
assert new.last?
new = ListMixin.create(:parent_id => 0)
assert_equal 1, new.pos
assert new.first?
assert new.last?
end
def test_insert_at
new = ListMixin.create(:parent_id => 20)
assert_equal 1, new.pos
new = ListMixin.create(:parent_id => 20)
assert_equal 2, new.pos
new = ListMixin.create(:parent_id => 20)
assert_equal 3, new.pos
new4 = ListMixin.create(:parent_id => 20)
assert_equal 4, new4.pos
new4.insert_at(3)
assert_equal 3, new4.pos
new.reload
assert_equal 4, new.pos
new.insert_at(2)
assert_equal 2, new.pos
new4.reload
assert_equal 4, new4.pos
new5 = ListMixin.create(:parent_id => 20)
assert_equal 5, new5.pos
new5.insert_at(1)
assert_equal 1, new5.pos
new4.reload
assert_equal 5, new4.pos
end
def test_delete_middle
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
ListMixin.find(2).destroy
assert_equal [1, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
assert_equal 1, ListMixin.find(1).pos
assert_equal 2, ListMixin.find(3).pos
assert_equal 3, ListMixin.find(4).pos
ListMixin.find(1).destroy
assert_equal [3, 4], ListMixin.where(:parent_id => 5).map(&:id)
assert_equal 1, ListMixin.find(3).pos
assert_equal 2, ListMixin.find(4).pos
end
def test_nil_scope
new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
new2.move_higher
assert_equal [new2, new1, new3], ListMixin.where(:parent_id => nil)
end
def test_remove_from_list_should_then_fail_in_list?
assert_equal true, ListMixin.find(1).in_list?
ListMixin.find(1).remove_from_list
assert_equal false, ListMixin.find(1).in_list?
end
def test_remove_from_list_should_set_position_to_nil
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
ListMixin.find(2).remove_from_list
assert_equal [1, 3, 4], ListMixin.where(:parent_id => 5).where('pos IS NOT NULL').map(&:id)
assert_equal 1, ListMixin.find(1).pos
assert_equal nil, ListMixin.find(2).pos
assert_equal 2, ListMixin.find(3).pos
assert_equal 3, ListMixin.find(4).pos
end
def test_remove_before_destroy_does_not_shift_lower_items_twice
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
ListMixin.find(2).remove_from_list
ListMixin.find(2).destroy
assert_equal [1, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
assert_equal 1, ListMixin.find(1).pos
assert_equal 2, ListMixin.find(3).pos
assert_equal 3, ListMixin.find(4).pos
end
def test_remove_from_list_by_updating_should_shift_lower_items
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
ListMixin.find(2).update_attributes! :parent_id => 6
assert_equal [1, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
assert_equal 1, ListMixin.find(1).pos
assert_equal 2, ListMixin.find(3).pos
assert_equal 3, ListMixin.find(4).pos
end
def test_move_to_new_list_by_updating_should_append
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
assert_equal [5, 6, 7, 8], ListMixin.where(:parent_id => 6).map(&:id)
ListMixin.find(2).update_attributes! :parent_id => 6
assert_equal [5, 6, 7, 8, 2], ListMixin.where(:parent_id => 6).map(&:id)
assert_equal 1, ListMixin.find(5).pos
assert_equal 2, ListMixin.find(6).pos
assert_equal 3, ListMixin.find(7).pos
assert_equal 4, ListMixin.find(8).pos
assert_equal 5, ListMixin.find(2).pos
end
def test_before_destroy_callbacks_do_not_update_position_to_nil_before_deleting_the_record
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
# We need to trigger all the before_destroy callbacks without actually
# destroying the record so we can see the affect the callbacks have on
# the record.
list = ListMixin.find(2)
if list.respond_to?(:run_callbacks)
list.run_callbacks(:destroy)
else
list.send(:callback, :before_destroy)
end
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
assert_equal 1, ListMixin.find(1).pos
assert_equal 2, ListMixin.find(2).pos
assert_equal 2, ListMixin.find(3).pos
assert_equal 3, ListMixin.find(4).pos
end
def test_higher_items
assert_equal [1, 2], ListMixin.find(3).higher_items.map(&:id)
assert_equal [], ListMixin.find(1).higher_items.map(&:id)
end
def test_lower_items
assert_equal [3, 4], ListMixin.find(2).lower_items.map(&:id)
assert_equal [], ListMixin.find(4).lower_items.map(&:id)
end
def test_moving_first_and_last_items_return_true
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
assert_equal true, ListMixin.find(1).move_to_top
assert_equal true, ListMixin.find(1).move_higher
assert_equal true, ListMixin.find(4).move_to_bottom
assert_equal true, ListMixin.find(4).move_lower
end
def test_add_to_list_should_return_true
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
item = ListMixin.new(:parent_id => 5)
assert_equal true, item.new_record?
assert_equal false, item.in_list?
assert_equal true, item.add_to_list
item = ListMixin.create(:parent_id => 5)
item.remove_from_list
assert_equal false, item.new_record?
assert_equal false, item.in_list?
assert_equal true, item.add_to_list
end
def test_decrement_callbacks_should_return_true
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
item = ListMixin.find(4)
assert_equal 4, item.pos
assert_equal true, item.send(:decrement_position_on_lower_items)
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
item = ListMixin.find(4)
item.parent_id = 6
assert_equal 4, item.pos
assert_equal true, item.will_leave_list?
assert_equal true, item.send(:decrement_position_on_lower_items_in_old_list)
end
end
class ListWithStringScopeTest < ActiveSupport::TestCase
def setup
[5, 6].each do |parent_id|
(1..4).each do |i|
ListWithStringScopeMixin.create! :parent_id => parent_id
end
end
end
def test_insert
new = ListWithStringScopeMixin.create(:parent_id => 500)
assert_equal 1, new.pos
assert new.first?
assert new.last?
new = ListWithStringScopeMixin.create(:parent_id => 500)
assert_equal 2, new.pos
assert !new.first?
assert new.last?
new = ListWithStringScopeMixin.create(:parent_id => 500)
assert_equal 3, new.pos
assert !new.first?
assert new.last?
new = ListWithStringScopeMixin.create(:parent_id => 0)
assert_equal 1, new.pos
assert new.first?
assert new.last?
end
def test_remove_from_list_by_updating_should_shift_lower_items
assert_equal [1, 2, 3, 4], ListWithStringScopeMixin.where(:parent_id => 5).map(&:id)
ListWithStringScopeMixin.find(2).update_attributes! :parent_id => 6
assert_equal [1, 3, 4], ListWithStringScopeMixin.where(:parent_id => 5).map(&:id)
assert_equal 1, ListWithStringScopeMixin.find(1).pos
assert_equal 2, ListWithStringScopeMixin.find(3).pos
assert_equal 3, ListWithStringScopeMixin.find(4).pos
end
def test_move_to_new_list_by_updating_should_append
assert_equal [1, 2, 3, 4], ListWithStringScopeMixin.where(:parent_id => 5).map(&:id)
assert_equal [5, 6, 7, 8], ListWithStringScopeMixin.where(:parent_id => 6).map(&:id)
ListWithStringScopeMixin.find(2).update_attributes! :parent_id => 6
assert_equal [5, 6, 7, 8, 2], ListWithStringScopeMixin.where(:parent_id => 6).map(&:id)
assert_equal 1, ListWithStringScopeMixin.find(5).pos
assert_equal 2, ListWithStringScopeMixin.find(6).pos
assert_equal 3, ListWithStringScopeMixin.find(7).pos
assert_equal 4, ListWithStringScopeMixin.find(8).pos
assert_equal 5, ListWithStringScopeMixin.find(2).pos
end
end
class ListSubTest < ActiveSupport::TestCase
def setup
(1..4).each do |i|
klass = ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2)
klass.create! :pos => i, :parent_id => 5000
end
end
def test_sti_class
assert_instance_of ListMixinSub1, ListMixin.find(1)
assert_instance_of ListMixinSub2, ListMixin.find(2)
assert_instance_of ListMixinSub1, ListMixin.find(3)
assert_instance_of ListMixinSub2, ListMixin.find(4)
end
def test_reordering
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5000).map(&:id)
ListMixin.find(2).move_lower
assert_equal [1, 3, 2, 4], ListMixin.where(:parent_id => 5000).map(&:id)
ListMixin.find(2).move_higher
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5000).map(&:id)
ListMixin.find(1).move_to_bottom
assert_equal [2, 3, 4, 1], ListMixin.where(:parent_id => 5000).map(&:id)
ListMixin.find(1).move_to_top
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5000).map(&:id)
ListMixin.find(2).move_to_bottom
assert_equal [1, 3, 4, 2], ListMixin.where(:parent_id => 5000).map(&:id)
ListMixin.find(4).move_to_top
assert_equal [4, 1, 3, 2], ListMixin.where(:parent_id => 5000).map(&:id)
end
def test_bounds_checking
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5000).map(&:pos)
ListMixin.find(1).move_higher
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5000).map(&:pos)
ListMixin.find(4).move_lower
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5000).map(&:pos)
end
def test_move_to_bottom_with_next_to_last_item
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5000).map(&:id)
ListMixin.find(3).move_to_bottom
assert_equal [1, 2, 4, 3], ListMixin.where(:parent_id => 5000).map(&:id)
end
def test_next_prev
assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
assert_nil ListMixin.find(1).higher_item
assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
assert_nil ListMixin.find(4).lower_item
end
def test_injection
item = ListMixin.new(:parent_id => 1)
assert_equal({ :parent_id => 1 }, item.send(:scope_condition))
assert_equal("pos", item.send(:position_column))
end
def test_insert_at
new = ListMixin.create(:parent_id => 20)
assert_equal 1, new.pos
new = ListMixinSub1.create(:parent_id => 20)
assert_equal 2, new.pos
new = ListMixinSub2.create(:parent_id => 20)
assert_equal 3, new.pos
new4 = ListMixin.create(:parent_id => 20)
assert_equal 4, new4.pos
new4.insert_at(3)
assert_equal 3, new4.pos
new.reload
assert_equal 4, new.pos
new.insert_at(2)
assert_equal 2, new.pos
new4.reload
assert_equal 4, new4.pos
new5 = ListMixinSub1.create(:parent_id => 20)
assert_equal 5, new5.pos
new5.insert_at(1)
assert_equal 1, new5.pos
new4.reload
assert_equal 5, new4.pos
end
def test_delete_middle
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5000).map(&:id)
ListMixin.find(2).destroy
assert_equal [1, 3, 4], ListMixin.where(:parent_id => 5000).map(&:id)
assert_equal 1, ListMixin.find(1).pos
assert_equal 2, ListMixin.find(3).pos
assert_equal 3, ListMixin.find(4).pos
ListMixin.find(1).destroy
assert_equal [3, 4], ListMixin.where(:parent_id => 5000).map(&:id)
assert_equal 1, ListMixin.find(3).pos
assert_equal 2, ListMixin.find(4).pos
end
def test_higher_items
ListMixin.find(2).remove_from_list
assert_equal [1], ListMixin.find(3).higher_items.map(&:id)
assert_equal [], ListMixin.find(1).higher_items.map(&:id)
end
def test_lower_items
ListMixin.find(3).remove_from_list
assert_equal [4], ListMixin.find(2).lower_items.map(&:id)
assert_equal [], ListMixin.find(4).lower_items.map(&:id)
end
def test_list_class
assert_equal [1, 2, 3, 4], ListMixin.all.map(&:pos)
end
end
class ArrayScopeListTest < ActiveSupport::TestCase
def setup
['ParentClass', 'bananas'].each do |parent_type|
[5, 6].each do |parent_id|
(1..4).each do |i|
ArrayScopeListMixin.create!(
:pos => i,
:parent_id => parent_id,
:parent_type => parent_type
)
end
end
end
end
def conditions(options = {})
{ :parent_id => 5, :parent_type => 'ParentClass' }.merge(options)
end
def test_reordering
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(2).move_lower
assert_equal [1, 3, 2, 4], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(2).move_higher
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(1).move_to_bottom
assert_equal [2, 3, 4, 1], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(1).move_to_top
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(2).move_to_bottom
assert_equal [1, 3, 4, 2], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(4).move_to_top
assert_equal [4, 1, 3, 2], ArrayScopeListMixin.where(conditions).map(&:id)
end
def test_bounds_checking
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:pos)
ArrayScopeListMixin.find(1).move_higher
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:pos)
ArrayScopeListMixin.find(4).move_lower
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:pos)
end
def test_move_to_bottom_with_next_to_last_item
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(3).move_to_bottom
assert_equal [1, 2, 4, 3], ArrayScopeListMixin.where(conditions).map(&:id)
end
def test_next_prev
assert_equal ArrayScopeListMixin.find(2), ArrayScopeListMixin.find(1).lower_item
assert_nil ArrayScopeListMixin.find(1).higher_item
assert_equal ArrayScopeListMixin.find(3), ArrayScopeListMixin.find(4).higher_item
assert_nil ArrayScopeListMixin.find(4).lower_item
end
def test_injection
item = ArrayScopeListMixin.new(conditions)
assert_equal conditions, item.send(:scope_condition)
assert_equal "pos", item.send(:position_column)
end
def test_insert
new = ArrayScopeListMixin.create(conditions(:parent_id => 20))
assert_equal 1, new.pos
assert new.first?
assert new.last?
new = ArrayScopeListMixin.create(conditions(:parent_id => 20))
assert_equal 2, new.pos
assert !new.first?
assert new.last?
new = ArrayScopeListMixin.create(conditions(:parent_id => 20))
assert_equal 3, new.pos
assert !new.first?
assert new.last?
new = ArrayScopeListMixin.create(conditions(:parent_id => 0))
assert_equal 1, new.pos
assert new.first?
assert new.last?
end
def test_insert_at
new = ArrayScopeListMixin.create(conditions(:parent_id => 20))
assert_equal 1, new.pos
new = ArrayScopeListMixin.create(conditions(:parent_id => 20))
assert_equal 2, new.pos
new = ArrayScopeListMixin.create(conditions(:parent_id => 20))
assert_equal 3, new.pos
new4 = ArrayScopeListMixin.create(conditions(:parent_id => 20))
assert_equal 4, new4.pos
new4.insert_at(3)
assert_equal 3, new4.pos
new.reload
assert_equal 4, new.pos
new.insert_at(2)
assert_equal 2, new.pos
new4.reload
assert_equal 4, new4.pos
new5 = ArrayScopeListMixin.create(conditions(:parent_id => 20))
assert_equal 5, new5.pos
new5.insert_at(1)
assert_equal 1, new5.pos
new4.reload
assert_equal 5, new4.pos
end
def test_delete_middle
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(2).destroy
assert_equal [1, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
assert_equal 1, ArrayScopeListMixin.find(1).pos
assert_equal 2, ArrayScopeListMixin.find(3).pos
assert_equal 3, ArrayScopeListMixin.find(4).pos
ArrayScopeListMixin.find(1).destroy
assert_equal [3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
assert_equal 1, ArrayScopeListMixin.find(3).pos
assert_equal 2, ArrayScopeListMixin.find(4).pos
end
def test_remove_from_list_by_updating_scope_part_1_should_shift_lower_items
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(2).update_attributes! :parent_id => 6
assert_equal [1, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
assert_equal 1, ArrayScopeListMixin.find(1).pos
assert_equal 2, ArrayScopeListMixin.find(3).pos
assert_equal 3, ArrayScopeListMixin.find(4).pos
end
def test_remove_from_list_by_updating_scope_part_2_should_shift_lower_items
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(2).update_attributes! :parent_type => 'bananas'
assert_equal [1, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
assert_equal 1, ArrayScopeListMixin.find(1).pos
assert_equal 2, ArrayScopeListMixin.find(3).pos
assert_equal 3, ArrayScopeListMixin.find(4).pos
end
def test_remove_from_list_by_updating_complete_scope_should_shift_lower_items
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(2).update_attributes! :parent_id => 6, :parent_type => 'bananas'
assert_equal [1, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
assert_equal 1, ArrayScopeListMixin.find(1).pos
assert_equal 2, ArrayScopeListMixin.find(3).pos
assert_equal 3, ArrayScopeListMixin.find(4).pos
end
def test_move_to_new_list_by_updating_scope_part_1_should_append
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
assert_equal [5, 6, 7, 8], ArrayScopeListMixin.where(conditions(:parent_id => 6)).map(&:id)
ArrayScopeListMixin.find(2).update_attributes! :parent_id => 6
assert_equal [5, 6, 7, 8, 2], ArrayScopeListMixin.where(conditions(:parent_id => 6)).map(&:id)
assert_equal 1, ArrayScopeListMixin.find(5).pos
assert_equal 2, ArrayScopeListMixin.find(6).pos
assert_equal 3, ArrayScopeListMixin.find(7).pos
assert_equal 4, ArrayScopeListMixin.find(8).pos
assert_equal 5, ArrayScopeListMixin.find(2).pos
end
def test_move_to_new_list_by_updating_scope_part_2_should_append
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
assert_equal [9, 10, 11, 12], ArrayScopeListMixin.where(conditions(:parent_type => 'bananas')).map(&:id)
ArrayScopeListMixin.find(2).update_attributes! :parent_type => 'bananas'
assert_equal [9, 10, 11, 12, 2], ArrayScopeListMixin.where(conditions(:parent_type => 'bananas')).map(&:id)
assert_equal 1, ArrayScopeListMixin.find(9).pos
assert_equal 2, ArrayScopeListMixin.find(10).pos
assert_equal 3, ArrayScopeListMixin.find(11).pos
assert_equal 4, ArrayScopeListMixin.find(12).pos
assert_equal 5, ArrayScopeListMixin.find(2).pos
end
def test_move_to_new_list_by_updating_complete_scope_should_append
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
assert_equal [13, 14, 15, 16], ArrayScopeListMixin.where(:parent_id => 6, :parent_type => 'bananas').map(&:id)
ArrayScopeListMixin.find(2).update_attributes! :parent_id => 6, :parent_type => 'bananas'
assert_equal [13, 14, 15, 16, 2], ArrayScopeListMixin.where(:parent_id => 6, :parent_type => 'bananas').map(&:id)
assert_equal 1, ArrayScopeListMixin.find(13).pos
assert_equal 2, ArrayScopeListMixin.find(14).pos
assert_equal 3, ArrayScopeListMixin.find(15).pos
assert_equal 4, ArrayScopeListMixin.find(16).pos
assert_equal 5, ArrayScopeListMixin.find(2).pos
end
def test_remove_from_list_should_then_fail_in_list?
assert_equal true, ArrayScopeListMixin.find(1).in_list?
ArrayScopeListMixin.find(1).remove_from_list
assert_equal false, ArrayScopeListMixin.find(1).in_list?
end
def test_remove_from_list_should_set_position_to_nil
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(2).remove_from_list
assert_equal [1, 3, 4], ArrayScopeListMixin.where(conditions).where('pos IS NOT NULL').map(&:id)
assert_equal 1, ArrayScopeListMixin.find(1).pos
assert_equal nil, ArrayScopeListMixin.find(2).pos
assert_equal 2, ArrayScopeListMixin.find(3).pos
assert_equal 3, ArrayScopeListMixin.find(4).pos
end
def test_remove_before_destroy_does_not_shift_lower_items_twice
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
ArrayScopeListMixin.find(2).remove_from_list
ArrayScopeListMixin.find(2).destroy
assert_equal [1, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
assert_equal 1, ArrayScopeListMixin.find(1).pos
assert_equal 2, ArrayScopeListMixin.find(3).pos
assert_equal 3, ArrayScopeListMixin.find(4).pos
end
def test_higher_items
assert_equal [1, 2], ArrayScopeListMixin.find(3).higher_items.map(&:id)
assert_equal [], ArrayScopeListMixin.find(1).higher_items.map(&:id)
end
def test_lower_items
assert_equal [3, 4], ArrayScopeListMixin.find(2).lower_items.map(&:id)
assert_equal [], ArrayScopeListMixin.find(4).lower_items.map(&:id)
end
end
class AssociationScopeListTest < ActiveSupport::TestCase
def setup
(1..4).each do |i|
AssociationScopeListMixin.create!(
:pos => i,
:parent_id => 5
)
end
(1..4).each do |i|
PolymorphicAssociationScopeListMixin.create!(
:pos => i,
:parent_id => 5,
:parent_type => 'ParentClass'
)
end
end
def test_association_scope_is_configured
assert_equal :parent_id,
AssociationScopeListMixin.acts_as_list_options[:scope]
end
def test_polymorphic_association_scope_is_configured
assert_equal [:parent_id, :parent_type],
PolymorphicAssociationScopeListMixin.acts_as_list_options[:scope]
end
end