0

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?

lulalala
  • 17,572
  • 15
  • 110
  • 169

1 Answers1

0

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.

Oshan Wisumperuma
  • 1,808
  • 1
  • 18
  • 32