Skip to content

fix(adjacency): sort resources without a position last instead of raising - #228

Open
pcbeingused333 wants to merge 3 commits into
Rails-Designer:mainfrom
pcbeingused333:fix/adjacency-nil-position
Open

pcbeingused333 wants to merge 3 commits into
Rails-Designer:mainfrom
pcbeingused333:fix/adjacency-nil-position

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

adjacent_by :position sorts with sort_by on the raw position, with or without within:. A resource whose front matter has no position makes next and previous raise ArgumentError (comparison of NilClass with Integer, or comparison of Array with Array failed when 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 a within: group. Relation#order already keeps nils last in that order; sort_by does not keep equal keys in order, so the index is part of the key. Nothing changes when every resource has a position.

Tests

  • Flat adjacent_by :position: one unpositioned resource, and two (the same order as Relation#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 main and 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.

…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 eelcoj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Left a few minor things to check out @pcbeingused333

Comment thread lib/perron/resource/adjacency.rb Outdated

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove. Unless doing something really wonky, the rationale is in the commit message/tests, not in comments

Comment thread lib/perron/resource/adjacency.rb Outdated
# 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort_by_position = ->(resources) do

Comment thread lib/perron/resource/adjacency.rb Outdated
# 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

group_rank = grouping_order ? (grouping_order.index(...) || ... || Float::INFINITY) : group_value.to_s

Comment thread test/perron/resource/adjacency_test.rb Outdated
assert_equal second.id, unpositioned.previous.id
end

test "within: a resource without a position sorts last in its group instead of raising" do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge into a single "within" case; overlaps :146/:198

Positioned ties now fall back to the collection index so sort_by stays deterministic.
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks @eelcoj, done: the sort key in adjacency.rb is now [0, position, index], so entries without a position keep their original order. I added an assertion for the tie case; the suite passes (18/18).

This comment was drafted with Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants