Skip to content
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

Allow blocks for setting id #315

Closed
mikemarsian opened this issue Sep 10, 2018 · 1 comment
Closed

Allow blocks for setting id #315

mikemarsian opened this issue Sep 10, 2018 · 1 comment
Labels
enhancement New feature or request

Comments

@mikemarsian
Copy link

mikemarsian commented Sep 10, 2018

Currently we allow to customize any attribute by using blocks. I would like to extend the same behaviour to ids. Currently the only way to customize id is by using set_id :custom_id but this specifies an alternative method in the object. Instead, I would like to be able to customize id right here in the serializer, using something like this:

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attributes :name, :year
  set_id do |record|
    "This will be a string ID with #{record.id} inside"
  end

This is useful in situations when you have different types of objects (say, Movie, MovieBuilder, BaseMovie) being serialized using the same serializer (GeneralMovieSerializer), and you want to unify their ids setting code. What do you think?

@shishirmk
Copy link
Collaborator

That sounds like a good idea. We should support that. If you can work on it, go ahead and make a pull request.

@shishirmk shishirmk added the enhancement New feature or request label Sep 15, 2018
larissa added a commit to larissa/fast_jsonapi that referenced this issue Oct 5, 2018
Allow an ID of object to be customized directly on the serializer by
passing a block to `set_id` as opposed to only through a model property.

We already allow for attributes that do not have a model property of the
same name to be customized directly on the serializer using a block.

Closes Netflix#315
larissa added a commit to larissa/fast_jsonapi that referenced this issue Oct 5, 2018
Allow an ID of object to be customized directly on the serializer by
passing a block to `set_id` as opposed to only through a model property.

We already allow for attributes that do not have a model property of the
same name to be customized directly on the serializer using a block.

Closes Netflix#315
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants