yosiat / panko_serializer

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

How do I selectively display a key/value pair? #133

Open omarkamal opened 1 year ago

omarkamal commented 1 year ago

Say I have a serializer like this :

class Device < Panko::Serializer
  attributes :id, :name
  has_many :lights, serializer: LightSerializer
  has_many :fans, serializer: FanSerializer
  has_many :curtains, serializer: CurtainSerializer
end

now every device need not have all three, some might have just a fan and a light, some might have just a light.

I don't want to display empty arrays every single time. How do I selectively hide them if the array size is zero? I tried adding if: -> { object.fans.count > 0 } but that didn't seem to work, I still got an empty array.

kemalmutlu commented 1 year ago

I have the same problem. Any news?

yosiat commented 1 year ago

Hey,

There is no option to have dynamic filters like you ask, the only way to do filtering is Nested Filters or Filters For.

Dynamic filters like this, might be something that I'll add in the future, right now it's not planned.