Passing argument to grape-entity modules

2.7k views Asked by At

The requirements of my project are now forcing me to passing parameters to nested entities. I have an entity A and an entity B that show some information and needs the A identifier on the system to build them.

module Services
 module Trips
  class TripPreviewResponseEntity < Grape::Entity
   expose :id
   expose :title
   expose :duration
   expose :total_price
   expose :description
   expose :details
   expose :destinations, using: Destinations::DestinationResponseEntity
  end
 end
end

In the example above I want to do something like that:

expose :destinations, using: Destinations::DestinationResponseEntity, :trip_id => object.id

And in the nested Entity use the trip_id parameter option in this way:

expose :trip_info do |item,options|
   item.show(options[:trip_id])
end

But it's failing saying that object is not defined into the entity. Is there a way to perform this? Any idea?

1

There are 1 answers

0
Oshan Wisumperuma On
module Services
 module Trips
  class TripPreviewResponseEntity < Grape::Entity
   expose :id
   expose :title
   expose :duration
   expose :total_price
   expose :description
   expose :details
   expose :destinations do |trip, _options| 
     DestinationResponseEntity.represent(trip.destinations, trip_id: trip.id)
   end
  end
 end
end