From 8ee4afda79f412b699aeee987e2937890ea9becb Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Mon, 18 Nov 2019 11:02:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=91=E4=B8=8A=E5=AE=9E=E9=AA=8C=E5=AE=A4fo?= =?UTF-8?q?rk=E7=89=88=E6=9C=AC=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admins/laboratory_subjects_controller.rb | 2 +- app/services/subjects/copy_subject_service.rb | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/controllers/admins/laboratory_subjects_controller.rb b/app/controllers/admins/laboratory_subjects_controller.rb index e534c6b4b..866a20a76 100644 --- a/app/controllers/admins/laboratory_subjects_controller.rb +++ b/app/controllers/admins/laboratory_subjects_controller.rb @@ -10,7 +10,7 @@ class Admins::LaboratorySubjectsController < Admins::BaseController def create subject = Subject.find(params[:subject_id]) - Subjects::CopySubjectService.call(subject, current_user.id, current_laboratory) + Subjects::CopySubjectService.call(subject, current_user, current_laboratory) render_ok end diff --git a/app/services/subjects/copy_subject_service.rb b/app/services/subjects/copy_subject_service.rb index 7d32189a5..5f8481cae 100644 --- a/app/services/subjects/copy_subject_service.rb +++ b/app/services/subjects/copy_subject_service.rb @@ -1,9 +1,9 @@ class Subjects::CopySubjectService < ApplicationService - attr_reader :subject, :to_subject, :user_id, :laboratory + attr_reader :subject, :to_subject, :user, :laboratory - def initialize(subject, user_id, laboratory=nil) + def initialize(subject, user, laboratory=nil) @subject = subject - @user_id = user_id + @user = user @laboratory = laboratory subject_params = subject.attributes.dup.except('id', 'copy_subject_id', 'user_id', 'homepage_show') @to_subject = Subject.new(subject_params) @@ -21,7 +21,7 @@ class Subjects::CopySubjectService < ApplicationService # 复制实践课程表 def copy_subject! to_subject.copy_subject_id = subject.id - to_subject.user_id = user_id + to_subject.user_id = user.id to_subject.save! copy_stages_data!(subject, to_subject) @@ -34,7 +34,7 @@ class Subjects::CopySubjectService < ApplicationService subject.stages.each do |stage| to_stage = to_subject.stages.new to_stage.attributes = stage.attributes.dup.except('id', 'subject_id', 'user_id') - to_stage.user_id = user_id + to_stage.user_id = user.id to_stage.save! copy_stage_shixuns_data!(stage, to_stage) @@ -61,10 +61,15 @@ class Subjects::CopySubjectService < ApplicationService to_shixun.attributes = shixun.attributes.dup.except('id', 'user_id', 'identifier', 'homepage_show', 'use_scope', 'averge_star', 'myshixuns_count') to_shixun.identifier = Util::UUID.generate_identifier(Shixun, 8) - to_shixun.user_id = user_id + to_shixun.user_id = user.id if laboratory to_shixun.laboratory_id = laboratory.id end + # 复制版本库 + fork_repository_name = "#{user.login}/#{to_shixun.identifier}" + GitService.fork_repository(repo_path: "#{shixun.repo_name}.git", + fork_repository_path: (fork_repository_name + ".git")) + to_shixun.repo_name = fork_repository_name to_shixun.save! copy_shixun_info_data!(shixun, to_shixun) @@ -124,7 +129,7 @@ class Subjects::CopySubjectService < ApplicationService shixun.challenges.each do |challenge| to_challenge = to_shixun.challenges.new to_challenge.attributes = challenge.attributes.dup.except('id', 'shixun_id', 'praises_count', 'user_id', 'visits') - to_challenge.user_id = user_id + to_challenge.user_id = user.id to_challenge.shixun_id = to_shixun.id to_challenge.save! @@ -201,12 +206,12 @@ class Subjects::CopySubjectService < ApplicationService # 创建实训成员 def copy_shixun_members_data!(to_shixun) - to_shixun.shixun_members.create!(user_id: user_id, role: 1) + to_shixun.shixun_members.create!(user_id: user.id, role: 1) end # 创建课程成员 def copy_subject_members_data(to_subject) - to_subject.subject_members.create!(user_id: user_id, role: 1) + to_subject.subject_members.create!(user_id: user.id, role: 1) end end