Commit 3f2deb04 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor ProjectMembers and GroupMembers code

Refactor ProjectMembers and GroupMembers controllers.
Make them look alike. Move `params` for `MembersFinder` under
`initialize` to be consistent with `GroupMembersFinder`
and most of other finders.
Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent e7ba0e22
...@@ -5,6 +5,7 @@ module MembersPresentation ...@@ -5,6 +5,7 @@ module MembersPresentation
def present_members(members) def present_members(members)
preload_associations(members) preload_associations(members)
Gitlab::View::Presenter::Factory.new( Gitlab::View::Presenter::Factory.new(
members, members,
current_user: current_user, current_user: current_user,
......
...@@ -21,19 +21,26 @@ class Groups::GroupMembersController < Groups::ApplicationController ...@@ -21,19 +21,26 @@ class Groups::GroupMembersController < Groups::ApplicationController
def index def index
@sort = params[:sort].presence || sort_value_name @sort = params[:sort].presence || sort_value_name
@project = @group.projects.find(params[:project_id]) if params[:project_id] @project = @group.projects.find(params[:project_id]) if params[:project_id]
@members = find_members
@members = GroupMembersFinder
.new(@group, current_user, params: filter_params)
.execute(include_relations: requested_relations)
if can_manage_members if can_manage_members
@skip_groups = @group.related_group_ids @skip_groups = @group.related_group_ids
@invited_members = present_invited_members(@members)
@invited_members = @members.invite
@invited_members = @invited_members.search_invite_email(params[:search_invited]) if params[:search_invited].present?
@invited_members = present_invited_members(@invited_members)
end end
@members = @members.non_invite @members = present_group_members(@members.non_invite)
@members = present_group_members(@members)
@requesters = present_members( @requesters = present_members(
AccessRequestsFinder.new(@group).execute(current_user)) AccessRequestsFinder.new(@group).execute(current_user)
)
@group_member = @group.group_members.new @group_member = @group.group_members.new
end end
...@@ -43,30 +50,24 @@ class Groups::GroupMembersController < Groups::ApplicationController ...@@ -43,30 +50,24 @@ class Groups::GroupMembersController < Groups::ApplicationController
private private
def present_invited_members(members) def can_manage_members
invited_members = members.invite can?(current_user, :admin_group_member, @group)
if params[:search_invited].present?
invited_members = invited_members.search_invite_email(params[:search_invited])
end
present_members(invited_members
.page(params[:invited_members_page])
.per(MEMBER_PER_PAGE_LIMIT))
end end
def find_members def present_invited_members(invited_members)
filter_params = params.slice(:two_factor, :search).merge(sort: @sort) present_members(invited_members
GroupMembersFinder.new(@group, current_user, params: filter_params).execute(include_relations: requested_relations) .page(params[:invited_members_page])
.per(MEMBER_PER_PAGE_LIMIT))
end end
def can_manage_members def present_group_members(members)
can?(current_user, :admin_group_member, @group) present_members(members
.page(params[:page])
.per(MEMBER_PER_PAGE_LIMIT))
end end
def present_group_members(original_members) def filter_params
members = original_members.page(params[:page]).per(MEMBER_PER_PAGE_LIMIT) params.permit(:two_factor, :search).merge(sort: @sort)
present_members(members)
end end
end end
......
...@@ -17,8 +17,9 @@ class Projects::ProjectMembersController < Projects::ApplicationController ...@@ -17,8 +17,9 @@ class Projects::ProjectMembersController < Projects::ApplicationController
@group_links = @project.project_group_links @group_links = @project.project_group_links
@group_links = @group_links.search(params[:search]) if params[:search].present? @group_links = @group_links.search(params[:search]) if params[:search].present?
@project_members = MembersFinder.new(@project, current_user) @project_members = MembersFinder
.execute(include_relations: requested_relations, params: params.merge(sort: @sort)) .new(@project, current_user, params: filter_params)
.execute(include_relations: requested_relations)
@project_members = present_members(@project_members.page(params[:page])) @project_members = present_members(@project_members.page(params[:page]))
...@@ -43,12 +44,17 @@ class Projects::ProjectMembersController < Projects::ApplicationController ...@@ -43,12 +44,17 @@ class Projects::ProjectMembersController < Projects::ApplicationController
return render_404 return render_404
end end
redirect_to(project_project_members_path(project), redirect_to(project_project_members_path(project), notice: notice)
notice: notice)
end end
# MembershipActions concern # MembershipActions concern
alias_method :membershipable, :project alias_method :membershipable, :project
private
def filter_params
params.permit(:search).merge(sort: @sort)
end
end end
Projects::ProjectMembersController.prepend_if_ee('EE::Projects::ProjectMembersController') Projects::ProjectMembersController.prepend_if_ee('EE::Projects::ProjectMembersController')
...@@ -9,7 +9,6 @@ class GroupMembersFinder < UnionFinder ...@@ -9,7 +9,6 @@ class GroupMembersFinder < UnionFinder
# search: string # search: string
# created_after: datetime # created_after: datetime
# created_before: datetime # created_before: datetime
attr_reader :params attr_reader :params
def initialize(group, user = nil, params: {}) def initialize(group, user = nil, params: {})
...@@ -22,7 +21,6 @@ class GroupMembersFinder < UnionFinder ...@@ -22,7 +21,6 @@ class GroupMembersFinder < UnionFinder
def execute(include_relations: [:inherited, :direct]) def execute(include_relations: [:inherited, :direct])
group_members = group.members group_members = group.members
relations = [] relations = []
@params = params
return group_members if include_relations == [:direct] return group_members if include_relations == [:direct]
......
...@@ -4,17 +4,19 @@ class MembersFinder ...@@ -4,17 +4,19 @@ class MembersFinder
# Params can be any of the following: # Params can be any of the following:
# sort: string # sort: string
# search: string # search: string
attr_reader :params
def initialize(project, current_user) def initialize(project, current_user, params: {})
@project = project @project = project
@current_user = current_user
@group = project.group @group = project.group
@current_user = current_user
@params = params
end end
def execute(include_relations: [:inherited, :direct], params: {}) def execute(include_relations: [:inherited, :direct])
members = find_members(include_relations, params) members = find_members(include_relations)
filter_members(members, params) filter_members(members)
end end
def can?(*args) def can?(*args)
...@@ -25,7 +27,7 @@ class MembersFinder ...@@ -25,7 +27,7 @@ class MembersFinder
attr_reader :project, :current_user, :group attr_reader :project, :current_user, :group
def find_members(include_relations, params) def find_members(include_relations)
project_members = project.project_members project_members = project.project_members
project_members = project_members.non_invite unless can?(current_user, :admin_project, project) project_members = project_members.non_invite unless can?(current_user, :admin_project, project)
...@@ -39,7 +41,7 @@ class MembersFinder ...@@ -39,7 +41,7 @@ class MembersFinder
distinct_union_of_members(union_members) distinct_union_of_members(union_members)
end end
def filter_members(members, params) def filter_members(members)
members = members.search(params[:search]) if params[:search].present? members = members.search(params[:search]) if params[:search].present?
members = members.sort_by_attribute(params[:sort]) if params[:sort].present? members = members.sort_by_attribute(params[:sort]) if params[:sort].present?
members members
......
...@@ -110,7 +110,7 @@ describe MembersFinder, '#execute' do ...@@ -110,7 +110,7 @@ describe MembersFinder, '#execute' do
project.add_maintainer(user3) project.add_maintainer(user3)
member3 = project.add_maintainer(user4) member3 = project.add_maintainer(user4)
result = described_class.new(project, user2).execute(params: { search: user4.name }) result = described_class.new(project, user2, params: { search: user4.name }).execute
expect(result).to contain_exactly(member3) expect(result).to contain_exactly(member3)
end end
...@@ -120,7 +120,7 @@ describe MembersFinder, '#execute' do ...@@ -120,7 +120,7 @@ describe MembersFinder, '#execute' do
member2 = project.add_maintainer(user3) member2 = project.add_maintainer(user3)
member3 = project.add_maintainer(user4) member3 = project.add_maintainer(user4)
result = described_class.new(project, user2).execute(params: { sort: 'id_desc' }) result = described_class.new(project, user2, params: { sort: 'id_desc' }).execute
expect(result).to eq([member3, member2, member1]) expect(result).to eq([member3, member2, member1])
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment