summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2017-04-17 14:45:14 +0300
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2017-04-17 14:45:14 +0300
commit7de75920f768b62322a3706fa1ad01d0282a7396 (patch)
treee0a09e106344bfbbe9414eb231fba529e870e29d
parenteeaeb2752a589c9046659d58d4a3f6be8030b699 (diff)
downloadgitlab-ce-7de75920f768b62322a3706fa1ad01d0282a7396.tar.gz
Refactor controller code that creates project/group members
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r--app/controllers/concerns/membership_actions.rb12
-rw-r--r--app/controllers/groups/group_members_controller.rb15
-rw-r--r--app/controllers/projects/project_members_controller.rb12
-rw-r--r--app/models/project.rb2
-rw-r--r--app/services/members/create_service.rb8
-rw-r--r--changelogs/unreleased/dz-refactor-create-members.yml4
-rw-r--r--spec/controllers/projects/project_members_controller_spec.rb2
7 files changed, 25 insertions, 30 deletions
diff --git a/app/controllers/concerns/membership_actions.rb b/app/controllers/concerns/membership_actions.rb
index c13333641d3..41438f0afe6 100644
--- a/app/controllers/concerns/membership_actions.rb
+++ b/app/controllers/concerns/membership_actions.rb
@@ -1,6 +1,18 @@
module MembershipActions
extend ActiveSupport::Concern
+ def create
+ status = Members::CreateService.new(membershipable, current_user, params).execute
+
+ redirect_url = polymorphic_url([membershipable, :members])
+
+ if status
+ redirect_to redirect_url, notice: 'Users were successfully added.'
+ else
+ redirect_to redirect_url, alert: 'No users specified.'
+ end
+ end
+
def request_access
membershipable.request_access(current_user)
diff --git a/app/controllers/groups/group_members_controller.rb b/app/controllers/groups/group_members_controller.rb
index 00c50f9d0ad..e32c14abaf5 100644
--- a/app/controllers/groups/group_members_controller.rb
+++ b/app/controllers/groups/group_members_controller.rb
@@ -21,21 +21,6 @@ class Groups::GroupMembersController < Groups::ApplicationController
@group_member = @group.group_members.new
end
- def create
- if params[:user_ids].blank?
- return redirect_to(group_group_members_path(@group), alert: 'No users specified.')
- end
-
- @group.add_users(
- params[:user_ids].split(','),
- params[:access_level],
- current_user: current_user,
- expires_at: params[:expires_at]
- )
-
- redirect_to group_group_members_path(@group), notice: 'Users were successfully added.'
- end
-
def update
@group_member = @group.group_members.find(params[:id])
diff --git a/app/controllers/projects/project_members_controller.rb b/app/controllers/projects/project_members_controller.rb
index 6e158e685e9..faa517652cc 100644
--- a/app/controllers/projects/project_members_controller.rb
+++ b/app/controllers/projects/project_members_controller.rb
@@ -10,18 +10,6 @@ class Projects::ProjectMembersController < Projects::ApplicationController
redirect_to namespace_project_settings_members_path(@project.namespace, @project, sort: sort)
end
- def create
- status = Members::CreateService.new(@project, current_user, params).execute
-
- redirect_url = namespace_project_settings_members_path(@project.namespace, @project)
-
- if status
- redirect_to redirect_url, notice: 'Users were successfully added.'
- else
- redirect_to redirect_url, alert: 'No users or groups specified.'
- end
- end
-
def update
@project_member = @project.project_members.find(params[:id])
diff --git a/app/models/project.rb b/app/models/project.rb
index a160efba912..73593f04283 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -181,7 +181,7 @@ class Project < ActiveRecord::Base
delegate :name, to: :owner, allow_nil: true, prefix: true
delegate :count, to: :forks, prefix: true
delegate :members, to: :team, prefix: true
- delegate :add_user, to: :team
+ delegate :add_user, :add_users, to: :team
delegate :add_guest, :add_reporter, :add_developer, :add_master, to: :team
delegate :empty_repo?, to: :repository
diff --git a/app/services/members/create_service.rb b/app/services/members/create_service.rb
index e4b24ccef92..3a58f6c065d 100644
--- a/app/services/members/create_service.rb
+++ b/app/services/members/create_service.rb
@@ -1,9 +1,15 @@
module Members
class CreateService < BaseService
+ def initialize(source, current_user, params = {})
+ @source = source
+ @current_user = current_user
+ @params = params
+ end
+
def execute
return false if params[:user_ids].blank?
- project.team.add_users(
+ @source.add_users(
params[:user_ids].split(','),
params[:access_level],
expires_at: params[:expires_at],
diff --git a/changelogs/unreleased/dz-refactor-create-members.yml b/changelogs/unreleased/dz-refactor-create-members.yml
new file mode 100644
index 00000000000..8cff21eabb1
--- /dev/null
+++ b/changelogs/unreleased/dz-refactor-create-members.yml
@@ -0,0 +1,4 @@
+---
+title: Refactor code that creates project/group members
+merge_request: 10735
+author:
diff --git a/spec/controllers/projects/project_members_controller_spec.rb b/spec/controllers/projects/project_members_controller_spec.rb
index 416eaa0037e..d61fbc0def1 100644
--- a/spec/controllers/projects/project_members_controller_spec.rb
+++ b/spec/controllers/projects/project_members_controller_spec.rb
@@ -55,7 +55,7 @@ describe Projects::ProjectMembersController do
user_ids: '',
access_level: Gitlab::Access::GUEST
- expect(response).to set_flash.to 'No users or groups specified.'
+ expect(response).to set_flash.to 'No users specified.'
expect(response).to redirect_to(namespace_project_settings_members_path(project.namespace, project))
end
end