yosiat / panko_serializer

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

Specify serializer inside serializer file #54

Open Kadaaran opened 5 years ago

Kadaaran commented 5 years ago

Hi there ☀️

Is there a way I can choose inside a serializer to render another serializer based on an attribute ? Here is my code which is triggered into another serializer with has_many :activities, each_serializer: ActivitySerializer

class ActivitySerializer < Panko::Serializer
  attributes :id, :name, :max_capacity

  has_many :levels, each_serializer: LevelSerializer
end

But I'd like to filter has_many: levels based on another attribute called organization inside Level or at least to choose which levels I want to return.

Is it possible ?

Thanks again !

yosiat commented 4 years ago

@Kadaaran Hi!

If I understand you correctly you want to change the serializer of "levels" instead of LevelSerializer use other serializer based on level attributes?

If so, currently it's not supported since Panko don't have "dynamic runtime", Panko decides which serializers to choose before starting the serialization to optimize for performance.

It's possible to implement this with a bit of hack and not a natural way currently. If this feature is blocking you from using Panko, let's discuss on a proper solution and I will be happy to accept a PR.