Consider using the default entity defined with success MyEntity instead of duplicating it the calls to present
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
The following discussion from gitlab-ce!13501 should be addressed:
-
@smcgivern started a discussion: (+3 comments) I think this could be
opts = current_user&.admin? ? { with: Entities::UserWithAdmin } : {}, but I'm not sure if that's clearer. It does solve the repetition of the 'default' entity, though. Wdyt?
For instance:
desc 'Get a single user' do
success Entities::User
end
params do
requires :id, type: Integer, desc: 'The ID of the user'
end
get ":id" do
user = User.find_by(id: params[:id])
not_found!('User') unless user && can?(current_user, :read_user, user)
entity = current_user&.admin? ? Entities::UserWithAdmin : Entities::User
present user, with: entity
end
could be written:
desc 'Get a single user' do
success Entities::User
end
params do
requires :id, type: Integer, desc: 'The ID of the user'
end
get ":id" do
user = User.find_by(id: params[:id])
not_found!('User') unless user && can?(current_user, :read_user, user)
opts = current_user&.admin? ? { with: Entities::UserWithAdmin } : {}
present user, opts
end
and we would avoid the duplication.
/cc @smcgivern
Edited by 🤖 GitLab Bot 🤖