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
Jérome Perrin
gitlab-ce
Commits
8cde1e32
Commit
8cde1e32
authored
Oct 11, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use polymorphism for common attributes in `GroupChildEntity`
parent
bd8943f5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
17 deletions
+22
-17
app/serializers/group_child_entity.rb
app/serializers/group_child_entity.rb
+12
-16
spec/serializers/group_child_entity_spec.rb
spec/serializers/group_child_entity_spec.rb
+10
-1
No files found.
app/serializers/group_child_entity.rb
View file @
8cde1e32
...
@@ -6,33 +6,25 @@ class GroupChildEntity < Grape::Entity
...
@@ -6,33 +6,25 @@ class GroupChildEntity < Grape::Entity
:created_at
,
:updated_at
,
:avatar_url
:created_at
,
:updated_at
,
:avatar_url
expose
:type
do
|
instance
|
expose
:type
do
|
instance
|
instance
.
class
.
name
.
downcas
e
typ
e
end
end
expose
:can_edit
do
|
instance
|
expose
:can_edit
do
|
instance
|
return
false
unless
request
.
respond_to?
(
:current_user
)
return
false
unless
request
.
respond_to?
(
:current_user
)
if
project?
can?
(
request
.
current_user
,
"admin_
#{
type
}
"
,
instance
)
can?
(
request
.
current_user
,
:admin_project
,
instance
)
else
can?
(
request
.
current_user
,
:admin_group
,
instance
)
end
end
end
expose
:edit_path
do
|
instance
|
expose
:edit_path
do
|
instance
|
if
project?
# We know `type` will be one either `project` or `group`.
edit_project_path
(
instance
)
# The `edit_polymorphic_path` helper would try to call the path helper
else
# with a plural: `edit_groups_path(instance)` or `edit_projects_path(instance)`
edit_group_path
(
instance
)
# while our methods are `edit_group_path` or `edit_group_path`
end
public_send
(
"edit_
#{
type
}
_path"
,
instance
)
# rubocop:disable GitlabSecurity/PublicS
end
end
end
expose
:relative_path
do
|
instance
|
expose
:relative_path
do
|
instance
|
if
project?
polymorphic_path
(
instance
)
project_path
(
instance
)
else
group_path
(
instance
)
end
end
end
expose
:permission
do
|
instance
|
expose
:permission
do
|
instance
|
...
@@ -78,4 +70,8 @@ class GroupChildEntity < Grape::Entity
...
@@ -78,4 +70,8 @@ class GroupChildEntity < Grape::Entity
def
project?
def
project?
object
.
is_a?
(
Project
)
object
.
is_a?
(
Project
)
end
end
def
type
object
.
class
.
name
.
downcase
end
end
end
spec/serializers/group_child_entity_spec.rb
View file @
8cde1e32
require
'spec_helper'
require
'spec_helper'
describe
GroupChildEntity
do
describe
GroupChildEntity
do
include
Gitlab
::
Routing
.
url_helpers
let
(
:user
)
{
create
(
:user
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:request
)
{
double
(
'request'
)
}
let
(
:request
)
{
double
(
'request'
)
}
let
(
:entity
)
{
described_class
.
new
(
object
,
request:
request
)
}
let
(
:entity
)
{
described_class
.
new
(
object
,
request:
request
)
}
...
@@ -24,7 +26,6 @@ describe GroupChildEntity do
...
@@ -24,7 +26,6 @@ describe GroupChildEntity do
type
type
can_edit
can_edit
visibility
visibility
edit_path
permission
permission
relative_path]
.
each
do
|
attribute
|
relative_path]
.
each
do
|
attribute
|
it
"includes
#{
attribute
}
"
do
it
"includes
#{
attribute
}
"
do
...
@@ -51,6 +52,10 @@ describe GroupChildEntity do
...
@@ -51,6 +52,10 @@ describe GroupChildEntity do
expect
(
json
[
:star_count
]).
to
be_present
expect
(
json
[
:star_count
]).
to
be_present
end
end
it
'has the correct edit path'
do
expect
(
json
[
:edit_path
]).
to
eq
(
edit_project_path
(
object
))
end
it_behaves_like
'group child json'
it_behaves_like
'group child json'
end
end
...
@@ -87,6 +92,10 @@ describe GroupChildEntity do
...
@@ -87,6 +92,10 @@ describe GroupChildEntity do
expect
(
json
[
:can_leave
]).
to
be_truthy
expect
(
json
[
:can_leave
]).
to
be_truthy
end
end
it
'has the correct edit path'
do
expect
(
json
[
:edit_path
]).
to
eq
(
edit_group_path
(
object
))
end
it_behaves_like
'group child json'
it_behaves_like
'group child json'
end
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