Grape Entity expose to delegate/forward to other object

423 Views Asked by At

When using Grape Entity, sometimes we want to expose a few fields from an inner/related object. We can simply do this by:

expose(:bar) { |entity| entity.foo.bar }

However if many fields needs this treatment, this can get very repetitive. Is there a way to define delegation / forward so a bunch of expose all are associated with another object?

1

There are 1 best solutions below

0
On

create a separate entity for foo class, let's call it API::Entities::Foo. then you can

  expose :foo, using: API::Entities::Foo

you have to follow REST guidelines to serialize like this. you are dealing with resources. don't break it to flat, cumbersome data structures.