2019-01-15 20:45:05 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-27 22:18:26 +08:00
|
|
|
class ExperimentsController < ApplicationController
|
2017-01-06 19:56:14 +08:00
|
|
|
include SampleActions
|
2017-01-25 00:06:51 +08:00
|
|
|
include TeamsHelper
|
2017-01-04 22:04:12 +08:00
|
|
|
include InputSanitizeHelper
|
2017-04-06 20:55:18 +08:00
|
|
|
include ActionView::Helpers::TextHelper
|
|
|
|
include ApplicationHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
2016-10-12 15:30:55 +08:00
|
|
|
|
2016-08-08 21:04:02 +08:00
|
|
|
before_action :set_experiment,
|
2018-04-17 19:52:43 +08:00
|
|
|
except: %i(new create)
|
2016-08-08 21:04:02 +08:00
|
|
|
before_action :set_project,
|
2018-04-17 19:52:43 +08:00
|
|
|
only: %i(new create samples_index samples module_archive
|
|
|
|
clone_modal move_modal delete_samples)
|
2018-06-01 19:49:02 +08:00
|
|
|
before_action :load_projects_tree, only: %i(canvas samples module_archive)
|
2016-08-08 21:04:02 +08:00
|
|
|
before_action :check_view_permissions,
|
2018-04-17 19:52:43 +08:00
|
|
|
only: %i(canvas module_archive)
|
2018-02-19 21:47:36 +08:00
|
|
|
before_action :check_manage_permissions, only: :edit
|
|
|
|
before_action :check_archive_permissions, only: :archive
|
2018-02-07 18:49:15 +08:00
|
|
|
before_action :check_clone_permissions, only: %i(clone_modal clone)
|
|
|
|
before_action :check_move_permissions, only: %i(move_modal move)
|
2019-05-20 19:13:31 +08:00
|
|
|
before_action :set_inline_name_editing, only: %i(canvas module_archive)
|
2016-07-27 22:18:26 +08:00
|
|
|
|
2018-04-17 19:52:43 +08:00
|
|
|
layout 'fluid'.freeze
|
2016-07-28 17:30:34 +08:00
|
|
|
|
2017-01-06 19:56:14 +08:00
|
|
|
# Action defined in SampleActions
|
2017-01-12 21:08:29 +08:00
|
|
|
DELETE_SAMPLES = 'Delete'.freeze
|
2017-01-06 19:56:14 +08:00
|
|
|
|
2016-07-27 22:18:26 +08:00
|
|
|
def new
|
|
|
|
@experiment = Experiment.new
|
2016-07-28 20:44:44 +08:00
|
|
|
respond_to do |format|
|
2016-07-29 16:41:08 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'new_modal.html.erb'
|
|
|
|
)
|
2016-07-28 20:44:44 +08:00
|
|
|
}
|
2016-07-29 16:41:08 +08:00
|
|
|
end
|
2016-07-28 20:44:44 +08:00
|
|
|
end
|
2016-07-27 22:18:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@experiment = Experiment.new(experiment_params)
|
|
|
|
@experiment.created_by = current_user
|
2016-07-28 20:44:44 +08:00
|
|
|
@experiment.last_modified_by = current_user
|
|
|
|
@experiment.project = @project
|
2016-07-27 22:18:26 +08:00
|
|
|
if @experiment.save
|
2017-04-06 20:55:18 +08:00
|
|
|
experiment_annotation_notification
|
2019-03-05 18:39:57 +08:00
|
|
|
log_activity(:create_experiment)
|
2016-08-03 15:34:10 +08:00
|
|
|
flash[:success] = t('experiments.create.success_flash',
|
|
|
|
experiment: @experiment.name)
|
2016-09-02 17:46:48 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2017-04-20 23:55:28 +08:00
|
|
|
render json: { path: canvas_experiment_url(@experiment) }, status: :ok
|
2016-09-02 17:46:48 +08:00
|
|
|
end
|
|
|
|
end
|
2016-07-27 22:18:26 +08:00
|
|
|
else
|
2016-09-02 17:46:48 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: @experiment.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
2016-07-27 22:18:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-28 15:01:44 +08:00
|
|
|
def canvas
|
|
|
|
@project = @experiment.project
|
2017-10-06 18:43:27 +08:00
|
|
|
@active_modules = @experiment.active_modules
|
|
|
|
.includes(:tags, :inputs, :outputs)
|
2017-01-24 23:57:14 +08:00
|
|
|
current_team_switch(@project.team)
|
2016-07-27 22:18:26 +08:00
|
|
|
end
|
|
|
|
|
2016-08-03 15:34:10 +08:00
|
|
|
def edit
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'edit_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-27 22:18:26 +08:00
|
|
|
def update
|
2018-02-19 21:47:36 +08:00
|
|
|
render_403 && return unless if experiment_params[:archived] == 'false'
|
|
|
|
can_restore_experiment?(@experiment)
|
|
|
|
else
|
|
|
|
can_manage_experiment?(@experiment)
|
|
|
|
end
|
|
|
|
|
2017-04-06 20:55:18 +08:00
|
|
|
old_text = @experiment.description
|
2019-09-12 23:21:48 +08:00
|
|
|
@experiment.update(experiment_params)
|
2016-07-27 22:18:26 +08:00
|
|
|
@experiment.last_modified_by = current_user
|
2018-02-19 21:47:36 +08:00
|
|
|
|
2016-07-27 22:18:26 +08:00
|
|
|
if @experiment.save
|
2020-03-09 20:38:38 +08:00
|
|
|
experiment_annotation_notification(old_text) if old_text
|
2019-03-07 00:18:35 +08:00
|
|
|
|
2019-03-15 18:12:10 +08:00
|
|
|
activity_type = if experiment_params[:archived] == 'false'
|
|
|
|
:restore_experiment
|
|
|
|
else
|
|
|
|
:edit_experiment
|
|
|
|
end
|
2019-03-05 18:39:57 +08:00
|
|
|
log_activity(activity_type)
|
2019-03-07 00:18:35 +08:00
|
|
|
|
2016-09-02 20:13:59 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
2016-09-21 14:34:32 +08:00
|
|
|
format.html do
|
2019-03-06 16:17:34 +08:00
|
|
|
flash[:success] = t('experiments.update.success_flash',
|
|
|
|
experiment: @experiment.name)
|
2016-09-21 14:34:32 +08:00
|
|
|
redirect_to project_path(@experiment.project)
|
|
|
|
end
|
2016-09-02 20:13:59 +08:00
|
|
|
end
|
2016-07-27 22:18:26 +08:00
|
|
|
else
|
2016-09-02 20:13:59 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: @experiment.errors, status: :unprocessable_entity
|
|
|
|
end
|
2016-09-21 14:34:32 +08:00
|
|
|
format.html do
|
2019-03-06 16:17:34 +08:00
|
|
|
flash[:alert] = t('experiments.update.error_flash')
|
2017-12-08 00:59:23 +08:00
|
|
|
redirect_back(fallback_location: root_path)
|
2016-09-21 14:34:32 +08:00
|
|
|
end
|
2016-09-02 20:13:59 +08:00
|
|
|
end
|
2016-07-27 22:18:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-03 15:34:10 +08:00
|
|
|
def archive
|
2016-07-27 22:18:26 +08:00
|
|
|
@experiment.archived = true
|
|
|
|
@experiment.archived_by = current_user
|
|
|
|
@experiment.archived_on = DateTime.now
|
|
|
|
if @experiment.save
|
2019-03-05 18:39:57 +08:00
|
|
|
log_activity(:archive_experiment)
|
2016-08-03 15:34:10 +08:00
|
|
|
flash[:success] = t('experiments.archive.success_flash',
|
|
|
|
experiment: @experiment.name)
|
2016-08-05 21:23:01 +08:00
|
|
|
|
2016-08-03 15:34:10 +08:00
|
|
|
redirect_to project_path(@experiment.project)
|
2016-07-27 22:18:26 +08:00
|
|
|
else
|
2016-07-28 22:01:53 +08:00
|
|
|
flash[:alert] = t('experiments.archive.error_flash')
|
2017-12-08 00:59:23 +08:00
|
|
|
redirect_back(fallback_location: root_path)
|
2016-07-27 22:18:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-11 15:46:15 +08:00
|
|
|
# GET: clone_modal_experiment_path(id)
|
|
|
|
def clone_modal
|
2016-08-16 14:49:24 +08:00
|
|
|
@projects = @experiment.projects_with_role_above_user(current_user)
|
2016-08-11 15:46:15 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'clone_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST: clone_experiment(id)
|
|
|
|
def clone
|
2019-01-21 23:08:42 +08:00
|
|
|
service = Experiments::CopyExperimentAsTemplateService
|
|
|
|
.call(experiment_id: @experiment.id,
|
|
|
|
project_id: move_experiment_param,
|
|
|
|
user_id: current_user.id)
|
2016-08-11 23:00:33 +08:00
|
|
|
|
2019-01-21 23:08:42 +08:00
|
|
|
if service.succeed?
|
2016-08-11 21:13:36 +08:00
|
|
|
flash[:success] = t('experiments.clone.success_flash',
|
|
|
|
experiment: @experiment.name)
|
2019-01-21 23:08:42 +08:00
|
|
|
redirect_to canvas_experiment_path(service.cloned_experiment)
|
2016-08-11 21:13:36 +08:00
|
|
|
else
|
|
|
|
flash[:error] = t('experiments.clone.error_flash',
|
2016-08-16 15:15:29 +08:00
|
|
|
experiment: @experiment.name)
|
2016-08-11 21:13:36 +08:00
|
|
|
redirect_to project_path(@experiment.project)
|
|
|
|
end
|
2016-08-11 15:46:15 +08:00
|
|
|
end
|
|
|
|
|
2016-08-12 17:26:18 +08:00
|
|
|
# GET: move_modal_experiment_path(id)
|
|
|
|
def move_modal
|
2016-08-16 14:49:24 +08:00
|
|
|
@projects = @experiment.moveable_projects(current_user)
|
2016-08-12 17:26:18 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'move_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST: move_experiment(id)
|
|
|
|
def move
|
2019-01-15 20:45:05 +08:00
|
|
|
service = Experiments::MoveToProjectService
|
|
|
|
.call(experiment_id: @experiment.id,
|
|
|
|
project_id: move_experiment_param,
|
|
|
|
user_id: current_user.id)
|
2016-08-16 14:49:24 +08:00
|
|
|
|
2019-01-15 20:45:05 +08:00
|
|
|
if service.succeed?
|
2016-08-12 17:26:18 +08:00
|
|
|
flash[:success] = t('experiments.move.success_flash',
|
|
|
|
experiment: @experiment.name)
|
2019-01-15 20:45:05 +08:00
|
|
|
path = canvas_experiment_url(@experiment)
|
|
|
|
status = :ok
|
2016-08-12 17:26:18 +08:00
|
|
|
else
|
2019-01-15 20:45:05 +08:00
|
|
|
message = t('experiments.move.error_flash',
|
|
|
|
experiment: escape_input(@experiment.name))
|
|
|
|
status = :unprocessable_entity
|
2016-08-12 17:26:18 +08:00
|
|
|
end
|
2019-01-15 20:45:05 +08:00
|
|
|
|
|
|
|
render json: { message: message, path: path }, status: status
|
2016-08-12 17:26:18 +08:00
|
|
|
end
|
|
|
|
|
2016-08-08 21:04:02 +08:00
|
|
|
def module_archive
|
|
|
|
end
|
|
|
|
|
2016-08-02 14:44:07 +08:00
|
|
|
def samples
|
2016-08-02 15:13:30 +08:00
|
|
|
@samples_index_link = samples_index_experiment_path(@experiment,
|
|
|
|
format: :json)
|
2017-01-24 23:57:14 +08:00
|
|
|
@team = @experiment.project.team
|
2016-08-02 14:44:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def samples_index
|
2017-01-24 23:57:14 +08:00
|
|
|
@team = @experiment.project.team
|
2016-08-02 14:44:07 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2016-08-02 15:13:30 +08:00
|
|
|
format.json do
|
|
|
|
render json: ::SampleDatatable.new(view_context,
|
2017-01-24 23:57:14 +08:00
|
|
|
@team,
|
2016-08-03 18:57:51 +08:00
|
|
|
nil,
|
|
|
|
nil,
|
2016-12-21 21:32:28 +08:00
|
|
|
@experiment,
|
|
|
|
current_user)
|
2016-08-02 15:13:30 +08:00
|
|
|
end
|
2016-08-02 14:44:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-19 21:36:12 +08:00
|
|
|
def updated_img
|
2019-07-04 15:33:12 +08:00
|
|
|
if @experiment.workflowimg.attached? && !@experiment.workflowimg_exists?
|
|
|
|
@experiment.workflowimg.purge
|
2019-04-17 18:06:46 +08:00
|
|
|
@experiment.generate_workflow_img
|
|
|
|
end
|
2016-08-19 21:36:12 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2019-07-04 15:33:12 +08:00
|
|
|
if @experiment.workflowimg.attached?
|
2016-08-19 21:36:12 +08:00
|
|
|
render json: {}, status: 200
|
|
|
|
else
|
|
|
|
render json: {}, status: 404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-22 17:29:19 +08:00
|
|
|
def fetch_workflow_img
|
2016-08-19 21:36:12 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
2016-08-22 17:29:19 +08:00
|
|
|
workflowimg: render_to_string(
|
|
|
|
partial: 'projects/show/workflow_img.html.erb'
|
|
|
|
)
|
2016-08-19 21:36:12 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-27 22:18:26 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_experiment
|
|
|
|
@experiment = Experiment.find_by_id(params[:id])
|
2016-07-28 16:15:58 +08:00
|
|
|
render_404 unless @experiment
|
2016-07-27 22:18:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_project
|
2016-08-02 14:44:07 +08:00
|
|
|
@project = Project.find_by_id(params[:project_id]) || @experiment.project
|
2016-07-28 16:15:58 +08:00
|
|
|
render_404 unless @project
|
2016-07-27 22:18:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def experiment_params
|
2016-08-08 22:35:45 +08:00
|
|
|
params.require(:experiment).permit(:name, :description, :archived)
|
2016-07-27 22:18:26 +08:00
|
|
|
end
|
2016-07-28 17:00:56 +08:00
|
|
|
|
2019-01-15 20:45:05 +08:00
|
|
|
def move_experiment_param
|
|
|
|
params.require(:experiment).require(:project_id)
|
|
|
|
end
|
|
|
|
|
2018-06-01 19:49:02 +08:00
|
|
|
def load_projects_tree
|
2018-08-28 20:48:23 +08:00
|
|
|
# Switch to correct team
|
|
|
|
current_team_switch(@experiment.project.team) unless @experiment.project.nil?
|
2018-06-01 19:49:02 +08:00
|
|
|
@projects_tree = current_user.projects_tree(current_team, nil)
|
2018-03-22 18:41:33 +08:00
|
|
|
end
|
|
|
|
|
2016-07-28 16:15:58 +08:00
|
|
|
def check_view_permissions
|
2018-01-29 18:36:36 +08:00
|
|
|
render_403 unless can_read_experiment?(@experiment)
|
2016-08-08 21:04:02 +08:00
|
|
|
end
|
|
|
|
|
2018-02-19 21:47:36 +08:00
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_manage_experiment?(@experiment)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_archive_permissions
|
|
|
|
render_403 unless can_archive_experiment?(@experiment)
|
|
|
|
end
|
|
|
|
|
2018-02-07 18:49:15 +08:00
|
|
|
def check_clone_permissions
|
|
|
|
render_403 unless can_clone_experiment?(@experiment)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_move_permissions
|
|
|
|
render_403 unless can_move_experiment?(@experiment)
|
2016-08-12 17:26:18 +08:00
|
|
|
end
|
|
|
|
|
2019-05-20 19:13:31 +08:00
|
|
|
def set_inline_name_editing
|
|
|
|
return unless can_manage_experiment?(@experiment)
|
|
|
|
@inline_editable_title_config = {
|
|
|
|
name: 'title',
|
|
|
|
params_group: 'experiment',
|
2020-01-06 23:07:23 +08:00
|
|
|
item_id: @experiment.id,
|
2019-05-20 19:13:31 +08:00
|
|
|
field_to_udpate: 'name',
|
|
|
|
path_to_update: experiment_path(@experiment)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-04-06 20:55:18 +08:00
|
|
|
def experiment_annotation_notification(old_text = nil)
|
|
|
|
smart_annotation_notification(
|
|
|
|
old_text: old_text,
|
|
|
|
new_text: @experiment.description,
|
|
|
|
title: t('notifications.experiment_annotation_title',
|
|
|
|
experiment: @experiment.name,
|
|
|
|
user: current_user.full_name),
|
|
|
|
message: t('notifications.experiment_annotation_message_html',
|
|
|
|
project: link_to(@experiment.project.name,
|
|
|
|
project_url(@experiment.project)),
|
|
|
|
experiment: link_to(@experiment.name,
|
|
|
|
canvas_experiment_url(@experiment)))
|
|
|
|
)
|
|
|
|
end
|
2019-03-05 18:39:57 +08:00
|
|
|
|
|
|
|
def log_activity(type_of)
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
team: @experiment.project.team,
|
|
|
|
project: @experiment.project,
|
|
|
|
subject: @experiment,
|
|
|
|
message_items: { experiment: @experiment.id })
|
|
|
|
end
|
2016-07-27 22:18:26 +08:00
|
|
|
end
|