2017-09-05 09:30:16 +00:00
|
|
|
class GroupChildEntity < Grape::Entity
|
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
include RequestAwareEntity
|
|
|
|
|
2017-10-05 11:12:59 +00:00
|
|
|
expose :id, :name, :description, :visibility, :full_name,
|
|
|
|
:created_at, :updated_at, :avatar_url
|
2017-09-05 14:18:24 +00:00
|
|
|
|
2017-10-05 11:12:59 +00:00
|
|
|
expose :type do |instance|
|
2017-10-11 12:39:23 +00:00
|
|
|
type
|
2017-09-05 09:30:16 +00:00
|
|
|
end
|
|
|
|
|
2017-10-05 11:12:59 +00:00
|
|
|
expose :can_edit do |instance|
|
2017-09-05 09:30:16 +00:00
|
|
|
return false unless request.respond_to?(:current_user)
|
|
|
|
|
2017-10-11 12:39:23 +00:00
|
|
|
can?(request.current_user, "admin_#{type}", instance)
|
2017-09-05 14:18:24 +00:00
|
|
|
end
|
|
|
|
|
2017-10-05 11:12:59 +00:00
|
|
|
expose :edit_path do |instance|
|
2017-10-11 12:39:23 +00:00
|
|
|
# We know `type` will be one either `project` or `group`.
|
|
|
|
# The `edit_polymorphic_path` helper would try to call the path helper
|
|
|
|
# with a plural: `edit_groups_path(instance)` or `edit_projects_path(instance)`
|
|
|
|
# while our methods are `edit_group_path` or `edit_group_path`
|
|
|
|
public_send("edit_#{type}_path", instance) # rubocop:disable GitlabSecurity/PublicSend
|
2017-09-05 14:18:24 +00:00
|
|
|
end
|
|
|
|
|
2017-10-05 11:12:59 +00:00
|
|
|
expose :relative_path do |instance|
|
2017-10-11 12:39:23 +00:00
|
|
|
polymorphic_path(instance)
|
2017-09-13 09:11:09 +00:00
|
|
|
end
|
|
|
|
|
2017-10-05 11:12:59 +00:00
|
|
|
expose :permission do |instance|
|
2017-10-05 08:38:05 +00:00
|
|
|
membership&.human_access
|
2017-09-05 14:18:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Project only attributes
|
|
|
|
expose :star_count,
|
|
|
|
if: lambda { |_instance, _options| project? }
|
|
|
|
|
|
|
|
# Group only attributes
|
2017-10-05 11:12:59 +00:00
|
|
|
expose :children_count, :parent_id, :project_count, :subgroup_count,
|
2017-09-05 14:18:24 +00:00
|
|
|
unless: lambda { |_instance, _options| project? }
|
|
|
|
|
2017-10-05 11:12:59 +00:00
|
|
|
expose :leave_path, unless: lambda { |_instance, _options| project? } do |instance|
|
|
|
|
leave_group_members_path(instance)
|
2017-09-05 14:18:24 +00:00
|
|
|
end
|
|
|
|
|
2017-10-05 11:12:59 +00:00
|
|
|
expose :can_leave, unless: lambda { |_instance, _options| project? } do |instance|
|
2017-10-05 08:38:05 +00:00
|
|
|
if membership
|
2017-09-12 08:51:34 +00:00
|
|
|
can?(request.current_user, :destroy_group_member, membership)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-05 11:12:59 +00:00
|
|
|
expose :number_projects_with_delimiter, unless: lambda { |_instance, _options| project? } do |instance|
|
|
|
|
number_with_delimiter(instance.project_count)
|
2017-09-05 14:18:24 +00:00
|
|
|
end
|
|
|
|
|
2017-10-05 11:12:59 +00:00
|
|
|
expose :number_users_with_delimiter, unless: lambda { |_instance, _options| project? } do |instance|
|
|
|
|
number_with_delimiter(instance.member_count)
|
2017-09-05 09:30:16 +00:00
|
|
|
end
|
2017-10-05 08:38:05 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def membership
|
|
|
|
return unless request.current_user
|
|
|
|
|
2017-10-10 14:25:26 +00:00
|
|
|
@membership ||= request.current_user.members.find_by(source: object)
|
2017-10-05 08:38:05 +00:00
|
|
|
end
|
2017-10-05 11:12:59 +00:00
|
|
|
|
|
|
|
def project?
|
|
|
|
object.is_a?(Project)
|
|
|
|
end
|
2017-10-11 12:39:23 +00:00
|
|
|
|
|
|
|
def type
|
|
|
|
object.class.name.downcase
|
|
|
|
end
|
2017-09-05 09:30:16 +00:00
|
|
|
end
|