yosiat / panko_serializer

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

Add context to relations has_one/has_many #151

Open rus-max opened 1 year ago

rus-max commented 1 year ago

Hi!

Is it possible to add context to relations has_one/has_many?

class PostSerializer < Panko::Serializer
  attributes :context_attribute

  def context_attribute
    context[:date] || object.date
  end
end

class UserSerializer < Panko::Serializer
  attributes :name, :address
  has_many :posts, serializer: PostSerializer, context: { date: object.date }
end