Add support for autoloading nested related objects on ingredients - #72
Open
mamhoff wants to merge 7 commits into
Open
Add support for autoloading nested related objects on ingredients#72mamhoff wants to merge 7 commits into
mamhoff wants to merge 7 commits into
Conversation
tvdeyen
reviewed
Jul 20, 2023
tvdeyen
left a comment
Member
There was a problem hiding this comment.
Nice. I think the specs fail because we now always return public version elements.
mamhoff
force-pushed
the
better-autoloading
branch
from
July 20, 2023 15:20
aab986e to
4a273fb
Compare
3 tasks
tvdeyen
reviewed
Nov 8, 2024
tvdeyen
force-pushed
the
better-autoloading
branch
from
August 11, 2026 07:54
4a273fb to
8fb2cb2
Compare
tvdeyen
force-pushed
the
better-autoloading
branch
from
September 2, 2026 06:21
8731a0a to
5e1965a
Compare
This adds a `.preload_relations` object to serializers and leverages that and the Rails preloader service object to preload ActiveRecord object graphs below the related object of ingredients.
See stas/jsonapi.rb#91 for details on what these fix.
The pagination fixes imported from the jsonapi.rb PR left spaces inside the hash literal braces, tripping Standard's Layout/SpaceInsideHashLiteralBraces cop and blocking CI. Remove them to match the project's lint config.
Guard the serializer lookup with safe_constantize so an ingredient type without a matching JSON:API serializer is skipped rather than raising a NameError during preloading. Also drop the Rails 6 Preloader fallback, which is dead code since the gem requires alchemy_cms >= 8.2, and that in turn requires Rails 7.2 or newer.
The jsonapi_paginate and jsonapi_pagination_meta overrides were a workaround for jsonapi.rb losing the pre-pagination total when the collection is decorated after pagination (the pages controller maps the paginated result into api_page objects). That was fixed upstream by stas/jsonapi.rb#91, first released in jsonapi.rb 2.1.1, whose implementation is now identical to the overrides. Require >= 2.1.1 so the fix is guaranteed to be present and remove the redundant code.
The picture serializer hard-coded `preload_relations => [:thumbs]`, but `Alchemy::Picture` only defines the `thumbs` association under the Dragonfly storage adapter. On the default ActiveStorage adapter that association does not exist, so serializing a picture ingredient with a picture attached raised `ActiveRecord::AssociationNotFoundError` and returned a 500 for any request including ingredients. No spec attached a real picture, so the suite missed it. Instead of naming associations per serializer, ask each related object class to preload its own storage-specific associations through Alchemy's `alchemy_element_preloads` hook (the same mechanism core's ElementPreloader uses), which resolves to the correct associations for the active storage adapter and works for any relatable resource. This removes the `preload_relations` serializer API entirely.
Ingredient relationships are lazy-loaded, so their linkage and resources are
only emitted when the request's `include` asks for them. The scope, however,
eager loaded `{ingredients: :related_object}` on every request, materialising
ingredients and their related objects even for requests that never serialize
them. Gate that eager load (and the matching related-object preload) on the
same `include_ingredients?` predicate so both stay in sync and nothing is
fetched needlessly. The element tree stays eager because its linkage is always
present.
tvdeyen
force-pushed
the
better-autoloading
branch
from
September 4, 2026 10:05
44e8c31 to
76724a0
Compare
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.
This takes the capabilities from AlchemyCMS/alchemy_cms#2523 and allows pre-loading object graphs on ingredient's related objects.