From 25802d704376fe32e3daaa3f8f82e887ee04501d Mon Sep 17 00:00:00 2001 From: zmagoD Date: Sat, 12 Jun 2021 18:51:55 +0200 Subject: [PATCH] adopt move/clone my module logic to work with new user assignments (SCI-5805) --- app/models/experiment.rb | 3 +++ .../experiments/copy_experiment_as_template_service.rb | 9 ++++++++- app/services/experiments/move_to_project_service.rb | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 79aa102b4..e50957d8e 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -330,6 +330,9 @@ class Experiment < ApplicationRecord end next unless my_module.save + # regenerate user assignments + my_module.user_assignments.destroy_all + UserAssignments::GenerateUserAssignmentsJob.perform_later(my_module, current_user) Activities::CreateActivityService.call(activity_type: :move_task, owner: current_user, diff --git a/app/services/experiments/copy_experiment_as_template_service.rb b/app/services/experiments/copy_experiment_as_template_service.rb index 7b830a73a..dc5567598 100644 --- a/app/services/experiments/copy_experiment_as_template_service.rb +++ b/app/services/experiments/copy_experiment_as_template_service.rb @@ -38,7 +38,14 @@ module Experiments @c_exp.my_module_groups << g.deep_clone_to_experiment(@user, @c_exp) end - raise ActiveRecord::Rollback unless @c_exp.save + # Copy user assignments to the experiment + @exp.user_assignments.map do |user_assignment| + new_user_assigment = user_assignment.dup + new_user_assigment.assignable = @c_exp + new_user_assigment.save! + end + + @c_exp.save! end @errors.merge!(@c_exp.errors.to_hash) unless @c_exp.valid? diff --git a/app/services/experiments/move_to_project_service.rb b/app/services/experiments/move_to_project_service.rb index 9e3d2ee1a..b35a1c01c 100644 --- a/app/services/experiments/move_to_project_service.rb +++ b/app/services/experiments/move_to_project_service.rb @@ -21,6 +21,7 @@ module Experiments @exp.project = @project @exp.my_modules.each do |my_module| + sync_user_assignments(my_module) new_tags = [] my_module.tags.each do |tag| new_tag = @project.tags.where.not(id: new_tags).find_by(name: tag.name, color: tag.color) @@ -33,6 +34,7 @@ module Experiments end @exp.save! + sync_user_assignments(@exp) rescue @errors.merge!(@exp.errors.to_hash) unless @exp.valid? raise ActiveRecord::Rollback @@ -69,6 +71,13 @@ module Experiments end end + def sync_user_assignments(object) + # remove user assignments where the user are not present on the project + object.user_assignments.destroy_all + + UserAssignments::GenerateUserAssignmentsJob.perform_later(object, @user) + end + def track_activity Activities::CreateActivityService .call(activity_type: :move_experiment,