Skip to content

Add conditional attribute rendering #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Nitemaeric
Copy link
Contributor

Context

The following issues go into the detail of the need for these changes:

Essentially, we need a way to render attributes only when they need to be included. This is useful for

Concerns and considerations

  • How much of an impact does this have on performance?
  • Are conditional attributes actually a good practice? Should a separate serializer be used instead?

Changes

  • Add the Attribute and Association classes.
  • Allow users to pass through if: and unless: Symbol or Proc values.

@Nitemaeric Nitemaeric self-assigned this May 3, 2025
@Nitemaeric
Copy link
Contributor Author

Attributes

Gem IPS Comparison Allocations Comparison
panko_serializer 0.8.3 426.260k ± 9.3% baseline 1.016k baseline
transmutation 0.5.1 301.750k ± 5.8% 1.41x slower 1.864k 1.83x more
active_model_serializers 0.10.15 229.911k ± 2.9% 1.85x slower 2.368k 2.33x more
jsonapi-serializer 2.2.0 211.662k ± 1.3% 2.01x slower 3.400k 3.35x more
representable 3.2.0 171.186k ± 1.3% 2.49x slower 4.304k 4.24x more
rabl 0.17.0 120.789k ± 2.5% 3.53x slower 5.856k 5.76x more
jbuilder 2.13.0 100.800k ± 2.7% 4.23x slower 2.960k 2.91x more

Has One / Belongs To

Gem IPS Comparison Allocations Comparison
panko_serializer 0.8.3 143.595k ± 2.4% baseline 4.128k 1.03x more
transmutation 0.5.1 117.557k ± 1.7% 1.22x slower 4.024k baseline
representable 3.2.0 87.700k ± 2.0% 1.64x slower 6.184k 1.54x more
active_model_serializers 0.10.15 66.353k ± 3.6% 2.16x slower 6.384k 1.59x more
rabl 0.17.0 64.435k ± 2.3% 2.23x slower 9.608k 2.39x more
jsonapi-serializer 2.2.0 53.159k ± 6.3% 2.70x slower 12.368k 3.07x more
jbuilder 2.13.0 44.750k ± 1.7% 3.21x slower 4.944k 1.23x more

Has Many

Gem IPS Comparison Allocations Comparison
panko_serializer 0.8.3 263.676k ± 5.6% baseline 1.376k baseline
transmutation 0.5.1 80.908k ± 2.6% 3.26x slower 6.048k 4.40x more
representable 3.2.0 57.544k ± 2.2% 4.58x slower 9.640k 7.01x more
active_model_serializers 0.10.15 46.549k ± 2.0% 5.66x slower 9.480k 6.89x more
jbuilder 2.13.0 43.691k ± 1.7% 6.04x slower 6.208k 4.51x more
jsonapi-serializer 2.2.0 40.179k ± 1.3% 6.56x slower 16.408k 11.92x more
rabl 0.17.0 30.535k ± 3.4% 8.64x slower 10.912k 7.93x more

While implementing this feature, I realised there is a bit of logic to
the conditional rendering. This made `Serializer#as_json` bloat in
complexity.

To address the added complexity, I extracted out the `Attribute` and
`Association` classes.

This provides an added benefit of the `Attribute` and `Association`
classes defining the `#included?` method, which checks depth vs
max_depth, along with user defined
conditions.
@Nitemaeric Nitemaeric force-pushed the add-conditional-attribute-rendering branch from 7f07505 to 4a01b6f Compare May 3, 2025 16:05
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.

1 participant