#283 √ resolved
Michael Klishin (antares)

[PULL] Make display pass all 'custom' options to serializing methods like to_json

Reported by Michael Klishin (antares) | April 25th, 2008 @ 10:03 AM | in 0.9.4

I had a need to limit serialized attributes of my model and was using display. It automatically called #to_json and I had no control over what args are passes to it (no args, actually).

From commit log:

Make #display method pass all 'unknown' options to serialization method.

Sometimes it is very useful to exclude some attributes of model from being serialized

when using display. For instance, one may exclude :locatable_type and :locatable_id

from serialization to JSON but add :locatable polymorphic association instead.

Another case is when to_json and so forth methods would benefit from

taking parameters. Using this option we can control

what #display passes to those methods in one single place

and escape dealing with case/when or if/else and manual

content type lookup.

Example:

display @locations, :except => [:locatable_type, :locatable_id], :include => [:locatable]

serializes object with polymorphic association, not raw locatable_* attributes.

This feature with custom options passed to serializing method introduced in c69348864

Spec and fixture controllers are there, as well as updates to #display documentation.

What do you think? Is it reasonable to have in core? It helps a lot when you need to have some control on what #display does on models.

Comments and changes to this ticket

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »