0

how can i expose revision property of VehicleDetails that will not appear on GET request, but will be obligatory on PATCH/POST (only writing operations)?

class VehicleDetails < Grape::Entity
  expose :id
  expose :name
  expose :type
  expose :revision
end
Kapitula Alexey
  • 380
  • 4
  • 15
  • I think the OP is trying to return these values as part of the response to a PATCH/POST, so I'm not sure the routes are necessary. – Joe Dec 28 '17 at 15:53
  • @engineersmnky if he wants to return the new `revision` value on PATCH/POST it has nothing to do with the params and everything to do with the response entity. – Joe Dec 28 '17 at 16:14
  • I guess we'll need the OP to clarify @engineersmnky since I read it as wanting to decide on exposure based on verb. – Joe Dec 28 '17 at 16:18
  • Please explain your intentions more clearly. Preferably with a minimalist example of the anticipated requests and responses – engineersmnky Dec 28 '17 at 16:22

1 Answers1

0
vehicle = { id: 1, name: 'LADA', type: 'washbowl', revision: 15 }

class VehicleDetails < Grape::Entity
  expose :id
  expose :name
  expose :type
  expose :revision, if: lambda { |vehicle, options| options[:show_rev] }
end

VehicleDetails.represent(vehicle, show_rev: true).as_json
# => {:id=>1, :name=>"LADA", :type=>"washbowl", :revision=>15}

VehicleDetails.represent(vehicle).as_json
# => {:id=>1, :name=>"LADA", :type=>"washbowl"}

VehicleDetails.represent(vehicle, show_rev: false).as_json
# => {:id=>1, :name=>"LADA", :type=>"washbowl"}

# # or
# present vehicle with: VehicleDetails, show_rev: true
Rodion V
  • 321
  • 3
  • 10