1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
class GroupChildEntity < Grape::Entity
include ActionView::Helpers::NumberHelper
include RequestAwareEntity
expose :id, :name, :description, :visibility, :full_name, :relative_path,
:created_at, :updated_at, :can_edit, :type, :avatar_url, :permission, :edit_path
def project?
object.is_a?(Project)
end
def type
object.class.name.downcase
end
def can_edit
return false unless request.respond_to?(:current_user)
if project?
can?(request.current_user, :admin_project, object)
else
can?(request.current_user, :admin_group, object)
end
end
def edit_path
if project?
edit_project_path(object)
else
edit_group_path(object)
end
end
def relative_path
if project?
project_path(object)
else
group_path(object)
end
end
def permission
return unless request&.current_user
request.current_user.members.find_by(source: object)&.human_access
end
# Project only attributes
expose :star_count,
if: lambda { |_instance, _options| project? }
# Group only attributes
expose :children_count, :leave_path, :parent_id, :number_projects_with_delimiter,
:number_users_with_delimiter, :project_count, :subgroup_count, :can_leave,
unless: lambda { |_instance, _options| project? }
def children_finder
@children_finder ||= GroupChildrenFinder.new(current_user: request.current_user,
parent_group: object)
end
def children_count
@children_count ||= children_finder.total_count
end
def project_count
@project_count ||= children_finder.project_count
end
def subgroup_count
@subgroup_count ||= children_finder.subgroup_count
end
def leave_path
leave_group_group_members_path(object)
end
def can_leave
if membership = object.members_and_requesters.find_by(user: request.current_user)
can?(request.current_user, :destroy_group_member, membership)
else
false
end
end
def number_projects_with_delimiter
number_with_delimiter(project_count)
end
def number_users_with_delimiter
number_with_delimiter(object.users.count)
end
end
|