Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
082c28cc
Commit
082c28cc
authored
Oct 05, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use `expose` with blocks instead of defining methods
parent
23af161b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
27 deletions
+26
-27
app/serializers/group_child_entity.rb
app/serializers/group_child_entity.rb
+26
-27
No files found.
app/serializers/group_child_entity.rb
View file @
082c28cc
...
@@ -2,44 +2,40 @@ class GroupChildEntity < Grape::Entity
...
@@ -2,44 +2,40 @@ class GroupChildEntity < Grape::Entity
include
ActionView
::
Helpers
::
NumberHelper
include
ActionView
::
Helpers
::
NumberHelper
include
RequestAwareEntity
include
RequestAwareEntity
expose
:id
,
:name
,
:description
,
:visibility
,
:full_name
,
:relative_path
,
expose
:id
,
:name
,
:description
,
:visibility
,
:full_name
,
:created_at
,
:updated_at
,
:
can_edit
,
:type
,
:avatar_url
,
:permission
,
:edit_path
:created_at
,
:updated_at
,
:
avatar_url
def
project?
expose
:type
do
|
instance
|
object
.
is_a?
(
Project
)
instance
.
class
.
name
.
downcase
end
def
type
object
.
class
.
name
.
downcase
end
end
def
can_edit
expose
:can_edit
do
|
instance
|
return
false
unless
request
.
respond_to?
(
:current_user
)
return
false
unless
request
.
respond_to?
(
:current_user
)
if
project?
if
project?
can?
(
request
.
current_user
,
:admin_project
,
object
)
can?
(
request
.
current_user
,
:admin_project
,
instance
)
else
else
can?
(
request
.
current_user
,
:admin_group
,
object
)
can?
(
request
.
current_user
,
:admin_group
,
instance
)
end
end
end
end
def
edit_path
expose
:edit_path
do
|
instance
|
if
project?
if
project?
edit_project_path
(
object
)
edit_project_path
(
instance
)
else
else
edit_group_path
(
object
)
edit_group_path
(
instance
)
end
end
end
end
def
relative_path
expose
:relative_path
do
|
instance
|
if
project?
if
project?
project_path
(
object
)
project_path
(
instance
)
else
else
group_path
(
object
)
group_path
(
instance
)
end
end
end
end
def
permission
expose
:permission
do
|
instance
|
membership
&
.
human_access
membership
&
.
human_access
end
end
...
@@ -48,15 +44,14 @@ class GroupChildEntity < Grape::Entity
...
@@ -48,15 +44,14 @@ class GroupChildEntity < Grape::Entity
if:
lambda
{
|
_instance
,
_options
|
project?
}
if:
lambda
{
|
_instance
,
_options
|
project?
}
# Group only attributes
# Group only attributes
expose
:children_count
,
:leave_path
,
:parent_id
,
:number_projects_with_delimiter
,
expose
:children_count
,
:parent_id
,
:project_count
,
:subgroup_count
,
:number_users_with_delimiter
,
:project_count
,
:subgroup_count
,
:can_leave
,
unless:
lambda
{
|
_instance
,
_options
|
project?
}
unless:
lambda
{
|
_instance
,
_options
|
project?
}
def
leave_path
expose
:leave_path
,
unless:
lambda
{
|
_instance
,
_options
|
project?
}
do
|
instance
|
leave_group_members_path
(
object
)
leave_group_members_path
(
instance
)
end
end
def
can_leave
expose
:can_leave
,
unless:
lambda
{
|
_instance
,
_options
|
project?
}
do
|
instance
|
if
membership
if
membership
can?
(
request
.
current_user
,
:destroy_group_member
,
membership
)
can?
(
request
.
current_user
,
:destroy_group_member
,
membership
)
else
else
...
@@ -64,12 +59,12 @@ class GroupChildEntity < Grape::Entity
...
@@ -64,12 +59,12 @@ class GroupChildEntity < Grape::Entity
end
end
end
end
def
number_projects_with_delimiter
expose
:number_projects_with_delimiter
,
unless:
lambda
{
|
_instance
,
_options
|
project?
}
do
|
instance
|
number_with_delimiter
(
object
.
project_count
)
number_with_delimiter
(
instance
.
project_count
)
end
end
def
number_users_with_delimiter
expose
:number_users_with_delimiter
,
unless:
lambda
{
|
_instance
,
_options
|
project?
}
do
|
instance
|
number_with_delimiter
(
object
.
member_count
)
number_with_delimiter
(
instance
.
member_count
)
end
end
private
private
...
@@ -79,4 +74,8 @@ class GroupChildEntity < Grape::Entity
...
@@ -79,4 +74,8 @@ class GroupChildEntity < Grape::Entity
@membership
||=
request
.
current_user
.
membership_for_object
(
object
)
@membership
||=
request
.
current_user
.
membership_for_object
(
object
)
end
end
def
project?
object
.
is_a?
(
Project
)
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment