Route element visibility through ElementsRepository - #196
Merged
Conversation
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.
sascha-karnatz
approved these changes
Aug 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 throughElementsRepository#visibleis behavior identical today — the repository callsto_aon the already loaded association and runs the same Ruby side select, so the eager load set up inPagesController#page_scope_with_includessurvives — 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
publishedscope 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.