Duck-type fields and add serializer context (#23, #21) - #32
Merged
Conversation
#23: Replace the attributes_config hash-of-options with Field objects — Attribute and Association — that each answer `render?` and `value`. This collapses the two conditionals in Serializer#as_json: def as_json(options = {}) fields.each_with_object({}) do |field, hash| hash[field.key] = field.value(self, options) if field.render?(self) end end The if/unless logic and the attribute-vs-association branch move onto the field objects. Each field precomputes its frozen JSON key once, removing the per-serialize `name.to_s` allocation. #21: Add `context` to serializers, defaulting to the caller of `serialize` (e.g. the controller), so serializers can reach `context.current_user`. Context threads through serialize, serialize_collection, associations (children inherit the caller's context), and render (overridable via `render json:, context:`).
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.
Context
Closes #23 and #21, building on the conditional-rendering work from #31.
Serializer#as_json(attribute-vs-association, and block-vs-plain) by giving attributes and associations objects that each respond to their own serialization logic.current_user) so conditions likeif: -> { current_user.admin? }are useful.The two compose: context is exactly what a field's
render?condition needs, so they land together.Changes
#23 — duck-typed fields
Transmutation::Fieldbase withAttributeandAssociationsubclasses. Each answersrender?(serializer)andvalue(serializer, options);Association#render?also enforcesmax_depth, andAssociation#valueserializes one level deeper.Serializer#as_jsoncollapses to the shape the issue described:if:/unless:handling, the attribute/association branch, and the block-vs-sendbranch all move onto the field objects;field_config/render_field?/field_value/evaluate_conditionare gone fromSerializer.name.to_sallocation, andas_jsoniterates field objects directly (fewer per-call allocations than the old pair destructuring).#21 — serializer context
context:, exposed as#context. It defaults to the caller ofserialize(the controller, sinceSerializationis mixed into controllers), socontext.current_userworks out of the box.serialize,serialize_collection, associations (children inherit the original caller's context), andrender(overridable withrender json:, context:).Housekeeping
Metrics/MethodLengthbudget in.rubocop_todo.ymlfrom 12 → 13: the old limit was pinned toas_json(now tiny after the refactor);renderis the current longest method.Testing
max_depth/collection specs still pass unchanged.main(head-vs-main table) to confirm the frozen-key/field-object changes don't regress and trim allocations.🤖 Generated with Claude Code
Generated by Claude Code