3

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!

PSR
  • 513
  • 6
  • 16

1 Answers1

4

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)
Yaro Holodiuk
  • 568
  • 6
  • 15
  • 1
    thank you @yaro this totally works! For others: be sure to include `, env: request.env` in the to_hash() if you require environment variables in your representer, like I did, e.g. for PATH_INFO – PSR May 12 '15 at 07:01