Skip to content

Commit

Permalink
Fixes .sort_by_ancestry with custom ancestry_column
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuru committed Mar 28, 2023
1 parent be23abb commit 009aafc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ancestry/class_methods.rb
Expand Up @@ -98,7 +98,7 @@ def sort_by_ancestry(nodes, &block)

unless arranged
presorted_nodes = nodes.sort do |a, b|
rank = (a.ancestry || ' ') <=> (b.ancestry || ' ')
rank = (a.public_send(ancestry_column) || ' ') <=> (b.public_send(ancestry_column) || ' ')
rank = yield(a, b) if rank == 0 && block_given?
rank
end
Expand Down
13 changes: 13 additions & 0 deletions test/concerns/sort_by_ancestry_test.rb
Expand Up @@ -233,4 +233,17 @@ def test_sort_by_ancestry_with_block_paginated_missing_parents_and_children
end
end
end

def test_sort_by_ancestry_with_ancestry_column
AncestryTestDatabase.with_model :ancestry_column => :t1, :extra_columns => {:rank => :integer} do |model|
_, n2, n3, n4, n5, _ = build_ranked_tree(model)

records = model.sort_by_ancestry(model.all.ordered_by_ancestry_and(:rank).offset(1).take(4), &RANK_SORT)
if CORRECT
assert_equal [n3, n2, n4, n5].map(&:id), records.map(&:id)
else
assert_equal [n2, n4, n5, n3].map(&:id), records.map(&:id)
end
end
end
end

0 comments on commit 009aafc

Please sign in to comment.