Refactor members controller destroy action

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2017-04-17 17:44:30 +03:00
parent 7de75920f7
commit 395fca1493
No known key found for this signature in database
GPG key ID: 627C5F589F467F17
4 changed files with 30 additions and 25 deletions

View file

@ -4,7 +4,7 @@ module MembershipActions
def create
status = Members::CreateService.new(membershipable, current_user, params).execute
redirect_url = polymorphic_url([membershipable, :members])
redirect_url = members_page_url
if status
redirect_to redirect_url, notice: 'Users were successfully added.'
@ -13,6 +13,20 @@ module MembershipActions
end
end
def destroy
Members::DestroyService.new(membershipable, current_user, params).
execute(:all)
respond_to do |format|
format.html do
message = "User was successfully removed from #{source_type}."
redirect_to members_page_url, notice: message
end
format.js { head :ok }
end
end
def request_access
membershipable.request_access(current_user)
@ -23,20 +37,20 @@ module MembershipActions
def approve_access_request
Members::ApproveAccessRequestService.new(membershipable, current_user, params).execute
redirect_to polymorphic_url([membershipable, :members])
redirect_to members_page_url
end
def leave
member = Members::DestroyService.new(membershipable, current_user, user_id: current_user.id).
execute(:all)
source_type = membershipable.class.to_s.humanize(capitalize: false)
notice =
if member.request?
"Your access request to the #{source_type} has been withdrawn."
else
"You left the \"#{membershipable.human_name}\" #{source_type}."
end
redirect_path = member.request? ? member.source : [:dashboard, membershipable.class.to_s.tableize]
redirect_to redirect_path, notice: notice
@ -47,4 +61,16 @@ module MembershipActions
def membershipable
raise NotImplementedError
end
def members_page_url
if membershipable.is_a?(Project)
project_settings_members_path(membershipable)
else
polymorphic_url([membershipable, :members])
end
end
def source_type
@source_type ||= membershipable.class.to_s.humanize(capitalize: false)
end
end

View file

@ -29,15 +29,6 @@ class Groups::GroupMembersController < Groups::ApplicationController
@group_member.update_attributes(member_params)
end
def destroy
Members::DestroyService.new(@group, current_user, id: params[:id]).execute(:all)
respond_to do |format|
format.html { redirect_to group_group_members_path(@group), notice: 'User was successfully removed from group.' }
format.js { head :ok }
end
end
def resend_invite
redirect_path = group_group_members_path(@group)

View file

@ -18,18 +18,6 @@ class Projects::ProjectMembersController < Projects::ApplicationController
@project_member.update_attributes(member_params)
end
def destroy
Members::DestroyService.new(@project, current_user, params).
execute(:all)
respond_to do |format|
format.html do
redirect_to namespace_project_settings_members_path(@project.namespace, @project)
end
format.js { head :ok }
end
end
def resend_invite
redirect_path = namespace_project_settings_members_path(@project.namespace, @project)

View file

@ -225,7 +225,7 @@ describe Projects::ProjectMembersController do
id: member
expect(response).to redirect_to(
namespace_project_project_members_path(project.namespace, project)
namespace_project_settings_members_path(project.namespace, project)
)
expect(project.members).to include member
end