@@ -13,35 +13,39 @@ def test_sort_by_ancestry
13
13
n3 = model . create! ( :parent => n2 )
14
14
n4 = model . create! ( :parent => n2 )
15
15
n5 = model . create! ( :parent => n1 )
16
+ n6 = model . create! ( :parent => n5 )
17
+ nodes = [ n1 , n2 , n3 , n4 , n5 , n6 ]
16
18
17
- records = model . sort_by_ancestry ( model . all . sort_by ( &:id ) . reverse )
18
- if records [ 1 ] == n2
19
- if records [ 2 ] == n3
20
- assert_equal [ n1 , n2 , n3 , n4 , n5 ] . map ( &:id ) , records . map ( &:id )
21
- else
22
- assert_equal [ n1 , n2 , n4 , n3 , n5 ] . map ( &:id ) , records . map ( &:id )
23
- end
24
- else
25
- if records [ 3 ] == n3
26
- assert_equal [ n1 , n5 , n2 , n3 , n4 ] . map ( &:id ) , records . map ( &:id )
27
- else
28
- assert_equal [ n1 , n5 , n2 , n4 , n3 ] . map ( &:id ) , records . map ( &:id )
29
- end
30
- end
19
+ # n1 needs to move to front, and n2 needs to move in front of n4, n3
20
+ assert_equal [ n1 , n5 , n6 , n2 , n4 , n3 ] . map ( &:id ) , model . sort_by_ancestry ( model . all . sort_by ( &:id ) . reverse ) . map ( &:id )
21
+ # none are parents
22
+ #assert_equal [n5, n4, n3].map(&:id), model.sort_by_ancestry([n5, n4, n3]).map(&:id)
23
+ # at the same level
24
+ assert_equal [ n3 , n4 ] . map ( &:id ) , model . sort_by_ancestry ( [ n3 , n4 ] ) . map ( &:id )
25
+ # n1 needs to move below both
26
+ assert_equal [ n1 , n5 , n2 ] . map ( &:id ) , model . sort_by_ancestry ( [ n5 , n2 , n1 ] ) . map ( &:id )
27
+ # n1 needs to move below even a double descendant
28
+ #assert_equal [n1, n5, n4].map(&:id), model.sort_by_ancestry([n5, n4, n1]).map(&:id)
31
29
end
32
30
end
33
31
34
32
def test_sort_by_ancestry_with_block
35
33
AncestryTestDatabase . with_model :extra_columns => { :rank => :integer } do |model |
34
+ # - n1 (0)
35
+ # - n5 (0)
36
+ # - n3 (1)
37
+ # - n2 (1)
38
+ # - n4 (0)
39
+ # - n6 (1)
36
40
n1 = model . create! ( :rank => 0 )
37
41
n2 = model . create! ( :rank => 1 )
38
- n3 = model . create! ( :rank => 0 , :parent => n1 )
42
+ n3 = model . create! ( :rank => 1 , :parent => n1 )
39
43
n4 = model . create! ( :rank => 0 , :parent => n2 )
40
- n5 = model . create! ( :rank => 1 , :parent => n1 )
44
+ n5 = model . create! ( :rank => 0 , :parent => n1 )
41
45
n6 = model . create! ( :rank => 1 , :parent => n2 )
42
46
43
47
records = model . sort_by_ancestry ( model . all . sort_by ( &:rank ) . reverse ) { |a , b | a . rank <=> b . rank }
44
- assert_equal [ n1 , n3 , n5 , n2 , n4 , n6 ] . map ( &:id ) , records . map ( &:id )
48
+ assert_equal [ n1 , n5 , n3 , n2 , n4 , n6 ] . map ( &:id ) , records . map ( &:id )
45
49
end
46
50
end
47
51
end
0 commit comments