2016-02-12 23:52:43 +08:00
|
|
|
class ProjectsController < ApplicationController
|
|
|
|
include SampleActions
|
2016-07-21 19:11:15 +08:00
|
|
|
include RenamingUtil
|
2017-01-25 00:06:51 +08:00
|
|
|
include TeamsHelper
|
2017-01-04 22:04:12 +08:00
|
|
|
include InputSanitizeHelper
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-07-28 16:15:58 +08:00
|
|
|
before_action :load_vars, only: [:show, :edit, :update,
|
2016-02-12 23:52:43 +08:00
|
|
|
:notifications, :reports,
|
2016-07-28 17:05:28 +08:00
|
|
|
:samples, :experiment_archive,
|
2016-02-12 23:52:43 +08:00
|
|
|
:delete_samples, :samples_index]
|
2016-07-28 16:15:58 +08:00
|
|
|
before_action :check_view_permissions, only: [:show, :reports,
|
2016-07-28 17:05:28 +08:00
|
|
|
:samples, :experiment_archive,
|
2016-02-12 23:52:43 +08:00
|
|
|
:samples_index]
|
|
|
|
before_action :check_view_notifications_permissions, only: [ :notifications ]
|
|
|
|
before_action :check_edit_permissions, only: [ :edit ]
|
2016-07-29 16:41:08 +08:00
|
|
|
before_action :check_experiment_archive_permissions,
|
|
|
|
only: [:experiment_archive]
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-10-21 15:06:19 +08:00
|
|
|
@filter_by_archived = false
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# except parameter could be used but it is not working.
|
|
|
|
layout :choose_layout
|
|
|
|
|
|
|
|
# Action defined in SampleActions
|
2017-01-12 21:08:29 +08:00
|
|
|
DELETE_SAMPLES = 'Delete'.freeze
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
def index
|
2017-01-24 23:57:14 +08:00
|
|
|
if params[:team]
|
2017-01-25 00:06:51 +08:00
|
|
|
current_team_switch(Team.find_by_id(params[:team]))
|
2016-10-12 15:30:55 +08:00
|
|
|
end
|
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
if current_user.teams.any?
|
|
|
|
@current_team_id = current_team.id if current_team
|
2016-10-25 03:20:41 +08:00
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
@current_team_id ||= current_user.teams.first.id
|
2016-10-20 19:13:44 +08:00
|
|
|
@current_sort = params[:sort].to_s
|
2017-01-25 00:06:51 +08:00
|
|
|
@projects_by_teams = current_user.projects_by_teams(@current_team_id,
|
|
|
|
@current_sort,
|
|
|
|
@filter_by_archived)
|
2017-01-30 23:54:22 +08:00
|
|
|
else
|
|
|
|
@projects_by_teams = []
|
2016-10-20 19:13:44 +08:00
|
|
|
end
|
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
@teams = current_user.teams
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# New project for create new project modal
|
|
|
|
@project = Project.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def archive
|
|
|
|
@filter_by_archived = true
|
|
|
|
index
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@project = Project.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@project = Project.new(project_params)
|
2016-10-12 18:56:44 +08:00
|
|
|
@project.created_by = current_user
|
2016-02-12 23:52:43 +08:00
|
|
|
@project.last_modified_by = current_user
|
2017-01-24 23:57:14 +08:00
|
|
|
if current_team.id == project_params[:team_id].to_i &&
|
2016-10-12 18:56:44 +08:00
|
|
|
@project.save
|
2016-02-12 23:52:43 +08:00
|
|
|
# Create user-project association
|
|
|
|
up = UserProject.new(
|
|
|
|
role: :owner,
|
|
|
|
user: current_user,
|
|
|
|
project: @project
|
|
|
|
)
|
|
|
|
up.save
|
|
|
|
|
|
|
|
# Create "project created" activity
|
|
|
|
Activity.create(
|
|
|
|
type_of: :create_project,
|
|
|
|
user: current_user,
|
|
|
|
project: @project,
|
|
|
|
message: t(
|
|
|
|
"activities.create_project",
|
|
|
|
user: current_user.full_name,
|
|
|
|
project: @project.name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
flash[:success] = t("projects.create.success_flash", name: @project.name)
|
|
|
|
respond_to do |format|
|
|
|
|
format.json {
|
|
|
|
render json: { url: projects_path }, status: :ok
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.json {
|
|
|
|
render json: @project.errors, status: :unprocessable_entity
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
respond_to do |format|
|
|
|
|
format.json {
|
|
|
|
render json: {
|
|
|
|
html: render_to_string({
|
|
|
|
partial: "edit.html.erb",
|
|
|
|
locals: { project: @project }
|
|
|
|
}),
|
2017-01-04 22:04:12 +08:00
|
|
|
title: t('projects.index.modal_edit_project.modal_title',
|
2017-01-11 22:50:11 +08:00
|
|
|
project: escape_input(@project.name))
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
return_error = false
|
|
|
|
flash_error = t('projects.update.error_flash', name: @project.name)
|
|
|
|
|
|
|
|
# Check archive permissions if archiving/restoring
|
|
|
|
if project_params.include? :archive
|
|
|
|
if (project_params[:archive] and !can_archive_project(@project)) or
|
|
|
|
(!project_params[:archive] and !can_restore_project(@project))
|
|
|
|
return_error = true
|
|
|
|
is_archive = URI(request.referer).path == projects_archive_path ? "restore" : "archive"
|
|
|
|
flash_error = t("projects.#{is_archive}.error_flash", name: @project.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
message_renamed = nil
|
|
|
|
message_visibility = nil
|
|
|
|
if project_params.include? :name and
|
|
|
|
project_params[:name] != @project.name then
|
|
|
|
message_renamed = t(
|
|
|
|
"activities.rename_project",
|
|
|
|
user: current_user.full_name,
|
|
|
|
project_old: @project.name,
|
|
|
|
project_new: project_params[:name]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if project_params.include? :visibility and
|
|
|
|
project_params[:visibility] != @project.visibility then
|
|
|
|
message_visibility = t(
|
|
|
|
"activities.change_project_visibility",
|
|
|
|
user: current_user.full_name,
|
|
|
|
project: @project.name,
|
|
|
|
visibility: project_params[:visibility] == "visible" ?
|
|
|
|
t("general.public") :
|
|
|
|
t("general.private")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@project.last_modified_by = current_user
|
|
|
|
if @project.update(project_params)
|
|
|
|
# Add activities if needed
|
2016-07-21 19:11:15 +08:00
|
|
|
if message_visibility.present?
|
2016-02-12 23:52:43 +08:00
|
|
|
Activity.create(
|
2016-07-21 19:11:15 +08:00
|
|
|
type_of: :change_project_visibility,
|
2016-02-12 23:52:43 +08:00
|
|
|
user: current_user,
|
|
|
|
project: @project,
|
2016-07-21 19:11:15 +08:00
|
|
|
message: message_visibility
|
2016-02-12 23:52:43 +08:00
|
|
|
)
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
if message_renamed.present?
|
2016-02-12 23:52:43 +08:00
|
|
|
Activity.create(
|
2016-07-21 19:11:15 +08:00
|
|
|
type_of: :rename_project,
|
2016-02-12 23:52:43 +08:00
|
|
|
user: current_user,
|
|
|
|
project: @project,
|
2016-07-21 19:11:15 +08:00
|
|
|
message: message_renamed
|
2016-02-12 23:52:43 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
flash_success = t('projects.update.success_flash', name: @project.name)
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
# Redirect URL for archive view is different as for other views.
|
|
|
|
if URI(request.referer).path == projects_archive_path
|
|
|
|
# The project should be restored
|
|
|
|
unless @project.archived
|
|
|
|
@project.restore(current_user)
|
|
|
|
|
|
|
|
# "Restore project" activity
|
|
|
|
Activity.create(
|
|
|
|
type_of: :restore_project,
|
|
|
|
user: current_user,
|
|
|
|
project: @project,
|
|
|
|
message: t(
|
|
|
|
"activities.restore_project",
|
|
|
|
user: current_user.full_name,
|
|
|
|
project: @project.name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
flash_success = t('projects.restore.success_flash',
|
|
|
|
name: @project.name)
|
|
|
|
end
|
|
|
|
redirect_to projects_archive_path
|
|
|
|
else
|
|
|
|
# The project should be archived
|
|
|
|
if @project.archived
|
|
|
|
@project.archive(current_user)
|
|
|
|
|
|
|
|
# "Archive project" activity
|
|
|
|
Activity.create(
|
|
|
|
type_of: :archive_project,
|
|
|
|
user: current_user,
|
|
|
|
project: @project,
|
|
|
|
message: t(
|
|
|
|
"activities.archive_project",
|
|
|
|
user: current_user.full_name,
|
|
|
|
project: @project.name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
flash_success = t('projects.archive.success_flash', name: @project.name)
|
|
|
|
end
|
|
|
|
redirect_to projects_path
|
|
|
|
end
|
|
|
|
flash[:success] = flash_success
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render json: {
|
|
|
|
status: :ok,
|
|
|
|
html: render_to_string({
|
|
|
|
partial: "projects/index/project.html.erb",
|
|
|
|
locals: { project: @project }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return_error = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if return_error then
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
flash[:error] = flash_error
|
|
|
|
# Redirect URL for archive view is different as for other views.
|
|
|
|
if URI(request.referer).path == projects_archive_path
|
|
|
|
redirect_to projects_archive_path
|
|
|
|
else
|
|
|
|
redirect_to projects_path
|
|
|
|
end
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render json: @project.errors,
|
|
|
|
status: :unprocessable_entity
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2017-05-15 15:54:37 +08:00
|
|
|
# save experiments order
|
|
|
|
if params[:sort]
|
|
|
|
@project.experiments_order = params[:sort].to_s
|
|
|
|
@project.save
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
# This is the "info" view
|
2017-01-24 23:57:14 +08:00
|
|
|
current_team_switch(@project.team)
|
2017-05-15 15:54:37 +08:00
|
|
|
@current_sort = @project.experiments_order
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def notifications
|
|
|
|
@modules = @project
|
|
|
|
.assigned_modules(current_user)
|
|
|
|
.order(due_date: :desc)
|
|
|
|
respond_to do |format|
|
|
|
|
#format.html
|
|
|
|
format.json {
|
|
|
|
render :json => {
|
|
|
|
:html => render_to_string({
|
|
|
|
:partial => "notifications.html.erb"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def samples
|
|
|
|
@samples_index_link = samples_index_project_path(@project, format: :json)
|
2017-01-24 23:57:14 +08:00
|
|
|
@team = @project.team
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2016-07-28 17:05:28 +08:00
|
|
|
def experiment_archive
|
2017-01-24 23:57:14 +08:00
|
|
|
current_team_switch(@project.team)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def samples_index
|
2017-01-24 23:57:14 +08:00
|
|
|
@team = @project.team
|
2016-12-21 21:32:28 +08:00
|
|
|
@user = current_user
|
2016-02-12 23:52:43 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2016-12-21 21:32:28 +08:00
|
|
|
format.json do
|
|
|
|
render json: ::SampleDatatable.new(view_context,
|
2017-01-24 23:57:14 +08:00
|
|
|
@team,
|
2016-12-21 21:32:28 +08:00
|
|
|
@project,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
@user)
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def project_params
|
2017-01-24 23:57:14 +08:00
|
|
|
params.require(:project).permit(:name, :team_id, :visibility, :archived)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_vars
|
|
|
|
@project = Project.find_by_id(params[:id])
|
|
|
|
|
|
|
|
unless @project
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_view_permissions
|
|
|
|
unless can_view_project(@project)
|
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_view_notifications_permissions
|
|
|
|
unless can_view_project_notifications(@project)
|
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_edit_permissions
|
|
|
|
unless can_edit_project(@project)
|
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-28 17:05:28 +08:00
|
|
|
def check_experiment_archive_permissions
|
2016-07-29 16:41:08 +08:00
|
|
|
render_403 unless can_view_project_archive(@project)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def choose_layout
|
|
|
|
action_name.in?(['index', 'archive']) ? 'main' : 'fluid'
|
|
|
|
end
|
|
|
|
end
|