diff --git a/app/services/experiments/move_to_project_service.rb b/app/services/experiments/move_to_project_service.rb index 9e3d2ee1a..1b7f36760 100644 --- a/app/services/experiments/move_to_project_service.rb +++ b/app/services/experiments/move_to_project_service.rb @@ -20,17 +20,8 @@ module Experiments ActiveRecord::Base.transaction do @exp.project = @project - @exp.my_modules.each do |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) - new_tag ||= - @project.tags.create!(name: tag.name, color: tag.color, created_by: @user, last_modified_by: @user) - new_tags << new_tag - end - my_module.tags.destroy_all - my_module.tags = new_tags - end + move_tags! + move_activities!(@exp) @exp.save! rescue @@ -69,6 +60,34 @@ module Experiments end end + def move_tags! + @exp.my_modules.each do |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) + new_tag ||= + @project.tags.create!(name: tag.name, color: tag.color, created_by: @user, last_modified_by: @user) + new_tags << new_tag + end + my_module.tags.destroy_all + my_module.tags = new_tags + end + end + + # recursively move all activities in child associations to new project + def move_activities!(subject) + Activity.where(subject: subject).update!(project: @project) + + child_associations = Extends::ACTIVITY_SUBJECT_CHILDREN[subject.class.name.underscore.to_sym] + return unless child_associations + + child_associations.each do |child_association| + [subject.public_send(child_association)].flatten.each do |child_subject| + move_activities!(child_subject) + end + end + end + def track_activity Activities::CreateActivityService .call(activity_type: :move_experiment, diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 0f0b53084..dc1e4389a 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -147,10 +147,10 @@ class Extends report: nil, project: nil, experiment: [:my_modules], - my_module: [:results, :protocols], - result: nil, + my_module: %i(results protocols), + result: [:asset], protocol: [:steps], - step: nil + step: [:assets] } ACTIVITY_MESSAGE_ITEMS_TYPES =