yosiat / panko_serializer

High Performance JSON Serialization for ActiveRecord & Ruby Objects
https://panko.dev
MIT License
592 stars 36 forks source link

Serialization of single object with context / options #83

Closed bbay closed 4 years ago

bbay commented 4 years ago

We already discussed serialization of a single object and the docs were updated with the example here.

Now, I face the situation where I need to pass some options and I noticed it is not possible due to the implementation in ResponseCreator (response.rb)

wdyt about adding this change?

   # in lib/panko/response.rb
    def self.serializer(data, serializer, options = {})
      json serializer.new(options).serialize_to_json(data)
    end

So I can then do something like this:

render(
  json: Panko::Response.create do |r|
    {
      status: 'active',
      user: r.serializer(user, UserSerializer, context: {index: true})
    }
  end
)

I could send a Pull Request if you think this is a good idea.

yosiat commented 4 years ago

Hi @bbay ,

Sounds good, I am always open to PR :)

bbay commented 4 years ago

Closed at #84 Thanks !! Waiting for that fancy preloader now! 😍