From cba6e9243620e2ddcb15c749d626156c5ce1c063 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 26 Feb 2013 23:14:32 +0200 Subject: [PATCH] move transfer logic out of project to service --- app/models/project.rb | 30 +++------------------ app/services/project_transfer_service.rb | 34 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 app/services/project_transfer_service.rb diff --git a/app/models/project.rb b/app/models/project.rb index 6da1b0b18ea..6ff2a3698df 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -247,32 +247,6 @@ class Project < ActiveRecord::Base users_projects.find_by_user_id(user_id) end - def transfer(new_namespace) - Project.transaction do - old_namespace = namespace - self.namespace = new_namespace - - old_dir = old_namespace.try(:path) || '' - new_dir = new_namespace.try(:path) || '' - - old_repo = if old_dir.present? - File.join(old_dir, self.path) - else - self.path - end - - if Project.where(path: self.path, namespace_id: new_namespace.try(:id)).present? - raise TransferError.new("Project with same path in target namespace already exists") - end - - Gitlab::ProjectMover.new(self, old_dir, new_dir).execute - - save! - end - rescue Gitlab::ProjectMover::ProjectMoveError => ex - raise Project::TransferError.new(ex.message) - end - def name_with_namespace @name_with_namespace ||= begin if namespace @@ -295,6 +269,10 @@ class Project < ActiveRecord::Base end end + def transfer(new_namespace) + ProjectTransferService.new.transfer(self, new_namespace) + end + def execute_hooks(data) hooks.each { |hook| hook.async_execute(data) } end diff --git a/app/services/project_transfer_service.rb b/app/services/project_transfer_service.rb new file mode 100644 index 00000000000..f91a3cd1992 --- /dev/null +++ b/app/services/project_transfer_service.rb @@ -0,0 +1,34 @@ +# ProjectTransferService class +# +# Used for transfer project to another namespace +# +class ProjectTransferService + attr_accessor :project + + def transfer(project, new_namespace) + Project.transaction do + old_namespace = project.namespace + project.namespace = new_namespace + + old_dir = old_namespace.try(:path) || '' + new_dir = new_namespace.try(:path) || '' + + old_repo = if old_dir.present? + File.join(old_dir, project.path) + else + project.path + end + + if Project.where(path: project.path, namespace_id: new_namespace.try(:id)).present? + raise TransferError.new("Project with same path in target namespace already exists") + end + + Gitlab::ProjectMover.new(project, old_dir, new_dir).execute + + save! + end + rescue Gitlab::ProjectMover::ProjectMoveError => ex + raise Project::TransferError.new(ex.message) + end +end +