Repository navigation
fix(adjacency): sort resources without a position last instead of raising - #228
pcbeingused333 wants to merge 3 commits into
Conversation
…sing adjacent_by sorted with sort_by on the raw position, so a resource whose front matter had no position made next/previous raise ArgumentError, with or without within. Missing positions now sort after real ones, in collection order. The index is part of the key because sort_by does not keep equal keys in order, matching Relation#order.
eelcoj
left a comment
There was a problem hiding this comment.
Thanks! Left a few minor things to check out @pcbeingused333
|
|
||
| group_order = if grouping_order | ||
| grouping_order.index(group_value.to_s) || grouping_order.index(group_value.to_s.to_sym) || Float::INFINITY | ||
| # A missing position sorts after real ones. The collection index is |
There was a problem hiding this comment.
Remove. Unless doing something really wonky, the rationale is in the commit message/tests, not in comments
| # A missing position sorts after real ones. The collection index is | ||
| # part of that key: sort_by does not keep equal keys in order, and | ||
| # Relation#order already keeps those nils in collection order. | ||
| ordered = ->(resources) do |
There was a problem hiding this comment.
sort_by_position = ->(resources) do
| # Relation#order already keeps those nils in collection order. | ||
| ordered = ->(resources) do | ||
| resources.sort_by.with_index do |resource, index| | ||
| value = resource.public_send(position_method) |
There was a problem hiding this comment.
position = resource.public_send(position_method)
position_key = position.nil? ? [1, index] : [0, position] # index missing on positioned ties
| position_key = value.nil? ? [1, index] : [0, value] | ||
|
|
||
| if grouping_method | ||
| group_value = resource.public_send(grouping_method) |
There was a problem hiding this comment.
group_rank = grouping_order ? (grouping_order.index(...) || ... || Float::INFINITY) : group_value.to_s
| assert_equal second.id, unpositioned.previous.id | ||
| end | ||
|
|
||
| test "within: a resource without a position sorts last in its group instead of raising" do |
There was a problem hiding this comment.
Merge into a single "within" case; overlaps :146/:198
Positioned ties now fall back to the collection index so sort_by stays deterministic.
|
Thanks @eelcoj, done: the sort key in This comment was drafted with Claude Code. |
adjacent_by :positionsorts withsort_byon the raw position, with or withoutwithin:. A resource whose front matter has nopositionmakesnextandpreviousraiseArgumentError(comparison of NilClass with Integer, orcomparison of Array with Array failedwhen grouped).Fix
Positioned resources sort by their position. Resources without one sort after them, in the order they appear in the collection. The sort key is
[0, position]when a position is present and[1, index]when it is not, including inside awithin:group.Relation#orderalready keeps nils last in that order;sort_bydoes not keep equal keys in order, so the index is part of the key. Nothing changes when every resource has a position.Tests
adjacent_by :position: one unpositioned resource, and two (the same order asRelation#order: positions 30, nil, 10, nil). Positioned resources come first; the unpositioned ones stay in collection order.within:: one unpositioned resource, and two in the same group. They sort last inside the group, in collection order.These cases raise on
mainand pass with this change.Disclosure
This change was prepared with AI assistance (Claude Code) and has not been reviewed line by line by a human before submission.