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
e013d398
Commit
e013d398
authored
Oct 05, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimize finding a membership for a user to avoid extra queries
parent
951abe2b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
7 deletions
+22
-7
app/models/user.rb
app/models/user.rb
+9
-0
app/serializers/group_child_entity.rb
app/serializers/group_child_entity.rb
+11
-5
spec/serializers/group_child_entity_spec.rb
spec/serializers/group_child_entity_spec.rb
+2
-2
No files found.
app/models/user.rb
View file @
e013d398
...
@@ -1073,6 +1073,15 @@ class User < ActiveRecord::Base
...
@@ -1073,6 +1073,15 @@ class User < ActiveRecord::Base
super
super
end
end
# Get the membership for an object without loading the source of the membership
#
# This loads the entire `members` relation of the user, used on `current_user`
def
membership_for_object
(
object
)
members
.
sort
(
'access_level_asc'
).
detect
do
|
member
|
object
.
is_a?
(
member
.
source_type
.
constantize
)
&&
member
.
source_id
==
object
.
id
end
end
protected
protected
# override, from Devise::Validatable
# override, from Devise::Validatable
...
...
app/serializers/group_child_entity.rb
View file @
e013d398
...
@@ -40,9 +40,7 @@ class GroupChildEntity < Grape::Entity
...
@@ -40,9 +40,7 @@ class GroupChildEntity < Grape::Entity
end
end
def
permission
def
permission
return
unless
request
&
.
current_user
membership
&
.
human_access
request
.
current_user
.
members
.
find_by
(
source:
object
)
&
.
human_access
end
end
# Project only attributes
# Project only attributes
...
@@ -55,11 +53,11 @@ class GroupChildEntity < Grape::Entity
...
@@ -55,11 +53,11 @@ class GroupChildEntity < Grape::Entity
unless:
lambda
{
|
_instance
,
_options
|
project?
}
unless:
lambda
{
|
_instance
,
_options
|
project?
}
def
leave_path
def
leave_path
leave_group_
group_
members_path
(
object
)
leave_group_members_path
(
object
)
end
end
def
can_leave
def
can_leave
if
membership
=
object
.
members_and_requesters
.
find_by
(
user:
request
.
current_user
)
if
membership
can?
(
request
.
current_user
,
:destroy_group_member
,
membership
)
can?
(
request
.
current_user
,
:destroy_group_member
,
membership
)
else
else
false
false
...
@@ -73,4 +71,12 @@ class GroupChildEntity < Grape::Entity
...
@@ -73,4 +71,12 @@ class GroupChildEntity < Grape::Entity
def
number_users_with_delimiter
def
number_users_with_delimiter
number_with_delimiter
(
object
.
member_count
)
number_with_delimiter
(
object
.
member_count
)
end
end
private
def
membership
return
unless
request
.
current_user
@membership
||=
request
.
current_user
.
membership_for_object
(
object
)
end
end
end
spec/serializers/group_child_entity_spec.rb
View file @
e013d398
...
@@ -34,7 +34,7 @@ describe GroupChildEntity do
...
@@ -34,7 +34,7 @@ describe GroupChildEntity do
end
end
describe
'for a project'
do
describe
'for a project'
do
s
et
(
:object
)
do
l
et
(
:object
)
do
create
(
:project
,
:with_avatar
,
create
(
:project
,
:with_avatar
,
description:
'Awesomeness'
)
description:
'Awesomeness'
)
end
end
...
@@ -55,7 +55,7 @@ describe GroupChildEntity do
...
@@ -55,7 +55,7 @@ describe GroupChildEntity do
end
end
describe
'for a group'
,
:nested_groups
do
describe
'for a group'
,
:nested_groups
do
s
et
(
:object
)
do
l
et
(
:object
)
do
create
(
:group
,
:nested
,
:with_avatar
,
create
(
:group
,
:nested
,
:with_avatar
,
description:
'Awesomeness'
)
description:
'Awesomeness'
)
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