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