2016-07-27 22:18:26 +08:00
|
|
|
class ExperimentsController < ApplicationController
|
|
|
|
include PermissionHelper
|
2016-08-08 21:04:02 +08:00
|
|
|
before_action :set_experiment,
|
|
|
|
except: [:new, :create]
|
|
|
|
before_action :set_project,
|
|
|
|
only: [:new, :create, :samples_index,
|
2016-08-12 17:26:18 +08:00
|
|
|
:samples, :module_archive, :clone_modal,
|
|
|
|
:move_modal]
|
2016-08-08 21:04:02 +08:00
|
|
|
before_action :check_view_permissions,
|
|
|
|
only: [:canvas, :module_archive]
|
|
|
|
before_action :check_module_archive_permissions,
|
|
|
|
only: [:module_archive]
|
2016-08-11 15:46:15 +08:00
|
|
|
before_action :check_experiment_clone_permissions,
|
|
|
|
only: [:clone_modal, :clone]
|
2016-08-12 17:26:18 +08:00
|
|
|
before_action :check_experiment_move_permissions,
|
|
|
|
only: [:move_modal, :move]
|
2016-07-27 22:18:26 +08:00
|
|
|
|
2016-07-28 17:00:56 +08:00
|
|
|
# except parameter could be used but it is not working.
|
|
|
|
layout :choose_layout
|
2016-07-28 17:30:34 +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
|
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
|
|
|
|
render json: {}, status: :ok
|
|
|
|
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
|
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
|
|
|
|
@experiment.update_attributes(experiment_params)
|
|
|
|
@experiment.last_modified_by = current_user
|
|
|
|
if @experiment.save
|
2016-08-25 15:11:42 +08:00
|
|
|
@experiment.touch(:workflowimg_updated_at)
|
2016-08-03 15:34:10 +08:00
|
|
|
flash[:success] = t('experiments.update.success_flash',
|
|
|
|
experiment: @experiment.name)
|
2016-09-21 14:34:32 +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
|
|
|
|
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-22 01:09:57 +08:00
|
|
|
flash[:alert] = t('experiments.update.error_flash')
|
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
|
|
|
|
redirect_to :back
|
|
|
|
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
|
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')
|
|
|
|
redirect_to :back
|
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
|
2016-08-24 17:09:03 +08:00
|
|
|
project = Project.find_by_id(params[:experiment].try(:[], :project_id))
|
2016-08-11 21:13:36 +08:00
|
|
|
|
2016-08-11 23:00:33 +08:00
|
|
|
# Try to clone the experiment
|
|
|
|
success = true
|
2016-08-16 14:49:24 +08:00
|
|
|
if @experiment.projects_with_role_above_user(current_user).include?(project)
|
2016-08-11 23:00:33 +08:00
|
|
|
cloned_experiment = @experiment.deep_clone_to_project(current_user,
|
|
|
|
project)
|
|
|
|
success = cloned_experiment.valid?
|
|
|
|
else
|
|
|
|
success = false
|
|
|
|
end
|
|
|
|
|
|
|
|
if success
|
|
|
|
Activity.create(
|
|
|
|
type_of: :clone_experiment,
|
|
|
|
project: project,
|
|
|
|
user: current_user,
|
|
|
|
message: I18n.t(
|
2016-08-16 15:15:29 +08:00
|
|
|
'activities.clone_experiment',
|
2016-08-11 23:00:33 +08:00
|
|
|
user: current_user.full_name,
|
|
|
|
experiment_new: cloned_experiment.name,
|
|
|
|
experiment_original: @experiment.name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2016-08-11 21:13:36 +08:00
|
|
|
flash[:success] = t('experiments.clone.success_flash',
|
|
|
|
experiment: @experiment.name)
|
2016-08-11 23:00:33 +08:00
|
|
|
redirect_to canvas_experiment_path(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
|
2016-08-24 17:09:03 +08:00
|
|
|
project = Project.find_by_id(params[:experiment].try(:[], :project_id))
|
2016-08-16 14:49:24 +08:00
|
|
|
old_project = @experiment.project
|
|
|
|
|
|
|
|
# Try to move the experiment
|
2016-08-12 17:26:18 +08:00
|
|
|
success = true
|
2016-08-16 14:49:24 +08:00
|
|
|
if @experiment.moveable_projects(current_user).include?(project)
|
|
|
|
success = @experiment.move_to_project(project)
|
|
|
|
else
|
|
|
|
success = false
|
|
|
|
end
|
|
|
|
|
2016-08-12 17:26:18 +08:00
|
|
|
if success
|
2016-08-16 14:49:24 +08:00
|
|
|
Activity.create(
|
|
|
|
type_of: :move_experiment,
|
|
|
|
project: project,
|
|
|
|
user: current_user,
|
|
|
|
message: I18n.t(
|
2016-08-16 15:15:29 +08:00
|
|
|
'activities.move_experiment',
|
2016-08-16 14:49:24 +08:00
|
|
|
user: current_user.full_name,
|
|
|
|
experiment: @experiment.name,
|
|
|
|
project_new: project.name,
|
|
|
|
project_original: old_project.name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2016-08-12 17:26:18 +08:00
|
|
|
flash[:success] = t('experiments.move.success_flash',
|
|
|
|
experiment: @experiment.name)
|
2016-09-02 22:31:17 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: { path: canvas_experiment_url(@experiment) }, status: :ok
|
|
|
|
end
|
|
|
|
end
|
2016-08-12 17:26:18 +08:00
|
|
|
else
|
2016-09-02 22:31:17 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: { message: t('experiments.move.error_flash',
|
|
|
|
experiment: @experiment.name) },
|
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
2016-08-12 17:26:18 +08:00
|
|
|
end
|
|
|
|
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)
|
2016-08-02 14:44:07 +08:00
|
|
|
@organization = @experiment.project.organization
|
|
|
|
end
|
|
|
|
|
|
|
|
def samples_index
|
|
|
|
@organization = @experiment.project.organization
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2016-08-02 15:13:30 +08:00
|
|
|
format.json do
|
|
|
|
render json: ::SampleDatatable.new(view_context,
|
|
|
|
@organization,
|
2016-08-03 18:57:51 +08:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
@experiment)
|
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
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2016-08-22 17:29:19 +08:00
|
|
|
if @experiment.workflowimg_updated_at.to_i >=
|
2016-08-19 21:36:12 +08:00
|
|
|
params[:timestamp].to_time.to_i
|
|
|
|
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
|
|
|
|
2016-07-28 16:15:58 +08:00
|
|
|
def check_view_permissions
|
|
|
|
render_403 unless can_view_experiment(@experiment)
|
|
|
|
end
|
|
|
|
|
2016-08-08 21:04:02 +08:00
|
|
|
def check_module_archive_permissions
|
|
|
|
render_403 unless can_view_experiment_archive(@experiment)
|
|
|
|
end
|
|
|
|
|
2016-08-11 15:46:15 +08:00
|
|
|
def check_experiment_clone_permissions
|
|
|
|
render_403 unless can_clone_experiment(@experiment)
|
|
|
|
end
|
|
|
|
|
2016-08-12 17:26:18 +08:00
|
|
|
def check_experiment_move_permissions
|
|
|
|
render_403 unless can_move_experiment(@experiment)
|
|
|
|
end
|
|
|
|
|
2016-07-28 17:00:56 +08:00
|
|
|
def choose_layout
|
2016-08-03 15:34:10 +08:00
|
|
|
action_name.in?(%w(index archive)) ? 'main' : 'fluid'
|
2016-07-28 17:00:56 +08:00
|
|
|
end
|
2016-07-27 22:18:26 +08:00
|
|
|
end
|