Custom options are accessible in serializer #512

Merged
merged 1 commit into from Aug 22, 2014

Conversation

Projects
None yet
5 participants
@TimPetricola
Contributor

TimPetricola commented Feb 14, 2014

Possible to access @options in a Serializer as it was in 0.8.

By doing so, you can use the options to filter attributes:

def filter(keys)
  keys.delete(:foo) if @options[:bar]
  keys
end
@joefiorini

This comment has been minimized.

Show comment
Hide comment
@joefiorini

joefiorini Feb 17, 2014

👍 I would like to see this as well. My use case:

I'm using CarrierWave and need to return image urls in my API. I'd like to have 1 serializer that renders an array of image urls. I was thinking of creating a declarative API for it like:

attribute :cover_image, serializer: ImageVersionSerializer, versions: [:thumb, :large]

But I can't specify versions without access to the options.

👍 I would like to see this as well. My use case:

I'm using CarrierWave and need to return image urls in my API. I'd like to have 1 serializer that renders an array of image urls. I was thinking of creating a declarative API for it like:

attribute :cover_image, serializer: ImageVersionSerializer, versions: [:thumb, :large]

But I can't specify versions without access to the options.

@ahx

This comment has been minimized.

Show comment
Hide comment
@ahx

ahx Mar 5, 2014

I have a similar use case where a serializer should render an array of things (time series elements) and i need to pass a time range parameter to the serializer.
I could not find a good solution for this without subclassing ActiveModel::Serializer and ActiveModel::ArraySerializer.
Having access to options would be really helpful. (+1)

ahx commented Mar 5, 2014

I have a similar use case where a serializer should render an array of things (time series elements) and i need to pass a time range parameter to the serializer.
I could not find a good solution for this without subclassing ActiveModel::Serializer and ActiveModel::ArraySerializer.
Having access to options would be really helpful. (+1)

@ahx

This comment has been minimized.

Show comment
Hide comment
@ahx

ahx Mar 5, 2014

Having a dedicated :context option for user defined options could be another option.
This way we would make sure that "reserved" options like meta, meta_key, root and scope don't cause conflicts.

ahx commented Mar 5, 2014

Having a dedicated :context option for user defined options could be another option.
This way we would make sure that "reserved" options like meta, meta_key, root and scope don't cause conflicts.

@TimPetricola

This comment has been minimized.

Show comment
Hide comment
@TimPetricola

TimPetricola Mar 10, 2014

Contributor

@ahx I really like this approach. Done 😉

Contributor

TimPetricola commented Mar 10, 2014

@ahx I really like this approach. Done 😉

@ahx

This comment has been minimized.

Show comment
Hide comment
@ahx

ahx Mar 10, 2014

Cool. Oat (https://github.com/ismasan/oat) is using this approach as well btw.

ahx commented Mar 10, 2014

Cool. Oat (https://github.com/ismasan/oat) is using this approach as well btw.

@timiyay

This comment has been minimized.

Show comment
Hide comment

timiyay commented May 1, 2014

+1

@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Jul 28, 2014

Contributor

I like this. Can I get a rebase, please?

Contributor

steveklabnik commented Jul 28, 2014

I like this. Can I get a rebase, please?

@TimPetricola

This comment has been minimized.

Show comment
Hide comment
@TimPetricola

TimPetricola Jul 28, 2014

Contributor

@steveklabnik Done 😄

Contributor

TimPetricola commented Jul 28, 2014

@steveklabnik Done 😄

@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Jul 28, 2014

Contributor

Thanks! Let's see what Travis says...

Contributor

steveklabnik commented Jul 28, 2014

Thanks! Let's see what Travis says...

steveklabnik added a commit that referenced this pull request Aug 22, 2014

Merge pull request #512 from TimPetricola/fix-options-access
Custom options are accessible in serializer

@steveklabnik steveklabnik merged commit ae7959b into rails-api:master Aug 22, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Aug 22, 2014

Contributor

Travis says yes! thank you! ❤️

Contributor

steveklabnik commented Aug 22, 2014

Travis says yes! thank you! ❤️

@TimPetricola

This comment has been minimized.

Show comment
Hide comment
@TimPetricola

TimPetricola Aug 22, 2014

Contributor

😄

Contributor

TimPetricola commented Aug 22, 2014

😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment