fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

Set id dynamically

Open JohnBDonner opened this issue 7 years ago • 0 comments

Being able to dynamically apply data passed in as options is super helpful:

link :personalized_url do |object, params|
  "http://movies.com/#{object.name}-#{params[:user].reference_code}"
end

However, I need to apply the id dynamically, which currently doesn't seem possible. Something like:

class SomethingSerializer
  include FastJsonapi::ObjectSerializer

  set_id do |_object, params|
    params[:id]
  end
end

# then...
SomethingSerializer.new(Something, { params: { id: my_custom_id } })

I saw this PR, https://github.com/Netflix/fast_jsonapi/pull/331, which I commented on, but figured it'd be good to start an issue too.

That PR was a step in the right direction, but I noticed this still doesn't exactly follow the same behaviour as attributes. You can't pass a custom params to the block like you can with attributes.

It seems the code for handling set_id is quite separate from attributes but maybe it's still possible to add that?

JohnBDonner avatar Jan 09 '19 20:01 JohnBDonner