Options grape representer

948 views Asked by At

I am working on a rails API with Representers, using the following gems: Grape, Roar and Grape-Roar

Now, I try to add conditions to include (or not include) certain properties in my representer based on a condition I pass from my API endpoint as described here (note the representable gem is used by the Roar gem)

I'm probably overlooking something, but I can't figure out how to pass options to my representer, so I can present properties based on a condition

For example, in one of my grape endpoints I call:

present payment_object, with: PaymentRepresenter, include_orders: true

to present a payment object with PaymentRepresenter. As you can see I want to include related order for the payment as well, so in my Payment representer I tried to do:

property :order, extend: OrderRepresenter, if: lambda { 
    |args| puts args[:include_orders] #just puts for testing
}

however args[:include_orders] just is nil

Does anyone know what I'm doing wrong here?

Thanks in advance!

1

There are 1 answers

1
Yaro Holodiuk On BEST ANSWER

I had this problem myself, and the only solution I came up with was to ditch nice idiomatic present..., with:... and manually extend my collection / record with representer, like this (concerning your example):

payment_object.extend(PaymentRepresenter).to_hash(include_orders: true)