mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-04-02 10:20:51 +08:00
Basic cloning of experiment works
Still missing: - cloning of assets - cloning of experiment image
This commit is contained in:
parent
7f21ae138a
commit
3b259d34b0
5 changed files with 90 additions and 2 deletions
app
config/locales
|
@ -90,6 +90,7 @@ class ExperimentsController < ApplicationController
|
|||
|
||||
# GET: clone_modal_experiment_path(id)
|
||||
def clone_modal
|
||||
@projects = projects_with_role_above_user
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
render json: {
|
||||
|
@ -103,7 +104,18 @@ class ExperimentsController < ApplicationController
|
|||
|
||||
# POST: clone_experiment(id)
|
||||
def clone
|
||||
redirect_to project_path(@experiment.project)
|
||||
project = Project.find_by_id(params[:experiment][:project_id])
|
||||
|
||||
if projects_with_role_above_user.include?(project)
|
||||
@experiment.deep_clone_to_project(current_user, project)
|
||||
flash[:success] = t('experiments.clone.success_flash',
|
||||
experiment: @experiment.name)
|
||||
redirect_to project_path(@experiment.project)
|
||||
else
|
||||
flash[:error] = t('experiments.clone.error_flash',
|
||||
experiment: @experiment.name)
|
||||
redirect_to project_path(@experiment.project)
|
||||
end
|
||||
end
|
||||
|
||||
def module_archive
|
||||
|
@ -161,4 +173,16 @@ class ExperimentsController < ApplicationController
|
|||
def choose_layout
|
||||
action_name.in?(%w(index archive)) ? 'main' : 'fluid'
|
||||
end
|
||||
|
||||
# Get projects where user is either owner or user in the same organization
|
||||
# as this experiment
|
||||
def projects_with_role_above_user
|
||||
organization = @experiment.project.organization
|
||||
current_user.user_projects
|
||||
.where(project:
|
||||
Project.where(organization: organization)
|
||||
.where(archived: false))
|
||||
.where('role < 2')
|
||||
.map(&:project)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -306,6 +306,30 @@ class Experiment < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
# Clone this experiment to given project
|
||||
def deep_clone_to_project(current_user, project)
|
||||
clone = Experiment.new(
|
||||
name: name + rand(1..1000).to_s,
|
||||
description: description,
|
||||
created_by: current_user,
|
||||
last_modified_by: current_user,
|
||||
project: project
|
||||
)
|
||||
|
||||
# Copy all workflows
|
||||
my_module_groups.each do |g|
|
||||
clone.my_module_groups << g.deep_clone_to_experiment(current_user, clone)
|
||||
end
|
||||
|
||||
# Copy modules without group
|
||||
clone.my_modules << modules_without_group.map do |m|
|
||||
m.deep_clone_to_experiment(current_user, clone)
|
||||
end
|
||||
clone.save
|
||||
|
||||
clone
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Archive all modules. Receives an array of module integer IDs.
|
||||
|
|
|
@ -286,10 +286,14 @@ class MyModule < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def deep_clone(current_user)
|
||||
deep_clone_to_experiment(current_user, self.experiment)
|
||||
end
|
||||
|
||||
def deep_clone_to_experiment(current_user, experiment)
|
||||
# Copy the module
|
||||
clone = MyModule.new(
|
||||
name: self.name,
|
||||
experiment: self.experiment,
|
||||
experiment: experiment,
|
||||
description: self.description,
|
||||
x: self.x,
|
||||
y: self.y)
|
||||
|
|
|
@ -46,4 +46,38 @@ class MyModuleGroup < ActiveRecord::Base
|
|||
def ordered_modules
|
||||
my_modules.order(workflow_order: :asc)
|
||||
end
|
||||
|
||||
def deep_clone_to_experiment(current_user, experiment)
|
||||
clone = MyModuleGroup.new(
|
||||
name: name,
|
||||
created_by: created_by,
|
||||
experiment: experiment
|
||||
)
|
||||
|
||||
# Get clones of modules from this group, save them as hash
|
||||
cloned_modules = ordered_modules.each_with_object({}) do |m, hash|
|
||||
hash[m.id] = m.deep_clone_to_experiment(current_user, experiment)
|
||||
hash
|
||||
end
|
||||
|
||||
ordered_modules.each do |m|
|
||||
# Copy connections
|
||||
m.inputs.each do |inp|
|
||||
Connection.create(
|
||||
input_id: cloned_modules[inp[:input_id]].id,
|
||||
output_id: cloned_modules[inp[:output_id]].id
|
||||
)
|
||||
end
|
||||
|
||||
# Copy remaining variables
|
||||
cloned_module = cloned_modules[m.id]
|
||||
cloned_module.my_module_group = self
|
||||
cloned_module.created_by = m.created_by
|
||||
cloned_module.workflow_order = m.workflow_order
|
||||
end
|
||||
|
||||
clone.my_modules << cloned_modules.values
|
||||
clone.save
|
||||
clone
|
||||
end
|
||||
end
|
||||
|
|
|
@ -619,6 +619,8 @@ en:
|
|||
modal_title: 'Clone experiment %{experiment}'
|
||||
label_title: 'Clone'
|
||||
modal_submit: 'Clone'
|
||||
success_flash: "Successfully cloned experiment %{experiment}"
|
||||
error_flash: 'Could not clone the experiment.'
|
||||
canvas:
|
||||
archive_confirm: "Are you sure to archive this experiment?"
|
||||
actions: 'Actions'
|
||||
|
|
Loading…
Add table
Reference in a new issue