Skip to content

Commit

Permalink
Merge pull request #656 from mitsuru/fix-sort_by_ancestry_with_ancest…
Browse files Browse the repository at this point in the history
…ry_column

Fixes `.sort_by_ancestry` with custom `ancestry_column`
  • Loading branch information
kbrock authored Mar 29, 2023
2 parents be23abb + 009aafc commit 257728b
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 257728b

Please sign in to comment.