想问一下ruby grape里以下的present,with,type都是什么意思?看了小半天也没懂。谢谢。
module API
class Statuses < Grape::API
version 'v1'
desc 'Statuses.', {
params: API::Entities::Status.documentation
}
get '/statuses' do
statuses = Status.all
type = current_user.admin? ? :full : :default
present statuses, with: API::Entities::Status, type: type
end
end
end
文档地址:https://github.com/intridea/grape-entity/blob/master/README.md
present is similar to rails’ render
with is to specify which Entity is used to process the data. You can think of Entity as the erb of json, which provides the template of json.
As for type, it is not a parameter of present, but a custom parameter passed to Entity. It will be passed to the inside of Entity for some processing. For example, to decide whether a certain attribute needs to be displayed, or to pass the block of expose as a
options
parameter, please refer to README https://github.com/intridea/grape-entity#conditional-exposure