Skip to content

Route element visibility through ElementsRepository - #196

Merged
tvdeyen merged 1 commit into
mainfrom
fix/elements-visible-through-repository
Aug 3, 2026
Merged

Route element visibility through ElementsRepository#196
tvdeyen merged 1 commit into
mainfrom
fix/elements-visible-through-repository

Conversation

@tvdeyen

@tvdeyen tvdeyen commented Aug 3, 2026

Copy link
Copy Markdown
Member

The JSON:API page decorator filtered visible elements itself before handing them to the repository, which left the public? iteration duplicated outside the abstraction that is meant to own it. Routing the filter through ElementsRepository#visible is behavior identical today — the repository calls to_a on the already loaded association and runs the same Ruby side select, so the eager load set up in PagesController#page_scope_with_includes survives — but it makes the repository the single choke point, so it can later own the preloading needed for visibility as well.

That eager load is easy to break by accident: applying the published scope to the already loaded association makes Rails run a fresh query per page and drop the nested element and ingredient includes, which is why the filtering happens in Ruby in the first place. Since nothing guarded against that, the index request spec now asserts that the number of element load queries does not grow with the number of pages.

The spec counts only full element record loads rather than every query touching alchemy_elements, because etag generation already fires one id-only query per page independently of this change.

The JSON:API page decorator filtered visible elements itself before
handing them to the repository, which left the `public?` iteration
duplicated outside the abstraction that is meant to own it. Routing the
filter through `ElementsRepository#visible` is behavior identical today
— the repository calls `to_a` on the already loaded association and runs
the same Ruby side select, so the controller's eager load survives — but
it makes the repository the single choke point, so it can later own the
preloading needed for visibility as well.

Because that eager load is easy to break by accident (applying the
`published` scope to the loaded association makes Rails re-query per
page and drop the nested element and ingredient includes), the index
request spec now asserts that the number of element load queries does
not grow with the number of pages.
@tvdeyen
tvdeyen requested a review from a team August 3, 2026 16:39
@tvdeyen
tvdeyen merged commit 19417e7 into main Aug 3, 2026
15 checks passed
@tvdeyen
tvdeyen deleted the fix/elements-visible-through-repository branch August 3, 2026 16:50
@tvdeyen tvdeyen added this to the 8.4 milestone Aug 3, 2026
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