Skip to content

Commit c3300b0

Browse files
committed
Fixup Sorting
Make sorting more consistent The 2 cases are - no rank function: only move parents to in front of children - rank function: sort each level with ranking function
1 parent cbaa20a commit c3300b0

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

Diff for: lib/ancestry/class_methods.rb

+6-10
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,13 @@ def arrange_serializable options={}, nodes=nil, &block
7070
# Pseudo-preordered array of nodes. Children will always follow parents,
7171
# for ordering nodes within a rank provide block, eg. Node.sort_by_ancestry(Node.all) {|a, b| a.rank <=> b.rank}.
7272
def sort_by_ancestry(nodes, &block)
73-
arranged = nodes if nodes.is_a?(Hash)
74-
75-
unless arranged
73+
if nodes.is_a?(Hash)
74+
arranged = nodes
75+
else
7676
presorted_nodes = nodes.sort do |a, b|
77-
a_cestry, b_cestry = a.ancestry || '0', b.ancestry || '0'
78-
79-
if block_given? && a_cestry == b_cestry
80-
yield a, b
81-
else
82-
a_cestry <=> b_cestry
83-
end
77+
r = a.ancestor_ids <=> b.ancestor_ids
78+
r = yield(a, b) if r == 0 && block_given?
79+
r
8480
end
8581

8682
arranged = arrange_nodes(presorted_nodes)

Diff for: test/concerns/sort_by_ancestry_test.rb

+21-17
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,39 @@ def test_sort_by_ancestry
1313
n3 = model.create!(:parent => n2)
1414
n4 = model.create!(:parent => n2)
1515
n5 = model.create!(:parent => n1)
16+
n6 = model.create!(:parent => n5)
17+
nodes = [n1, n2, n3, n4, n5, n6]
1618

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)
3129
end
3230
end
3331

3432
def test_sort_by_ancestry_with_block
3533
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)
3640
n1 = model.create!(:rank => 0)
3741
n2 = model.create!(:rank => 1)
38-
n3 = model.create!(:rank => 0, :parent => n1)
42+
n3 = model.create!(:rank => 1, :parent => n1)
3943
n4 = model.create!(:rank => 0, :parent => n2)
40-
n5 = model.create!(:rank => 1, :parent => n1)
44+
n5 = model.create!(:rank => 0, :parent => n1)
4145
n6 = model.create!(:rank => 1, :parent => n2)
4246

4347
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)
4549
end
4650
end
4751
end

0 commit comments

Comments
 (0)