2016-02-12 23:52:43 +08:00
|
|
|
class ResultAssetsController < ApplicationController
|
|
|
|
include ResultsHelper
|
|
|
|
|
|
|
|
before_action :load_vars, only: [:edit, :update, :download]
|
|
|
|
before_action :load_vars_nested, only: [:new, :create]
|
|
|
|
|
2018-02-09 23:14:40 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(new create edit update)
|
2016-02-12 23:52:43 +08:00
|
|
|
before_action :check_archive_permissions, only: [:update]
|
|
|
|
|
|
|
|
def new
|
|
|
|
@asset = Asset.new
|
|
|
|
@result = Result.new(
|
|
|
|
user: current_user,
|
|
|
|
my_module: @my_module,
|
|
|
|
asset: @asset
|
|
|
|
)
|
|
|
|
|
|
|
|
respond_to do |format|
|
2016-12-08 18:26:13 +08:00
|
|
|
format.json do
|
2016-02-12 23:52:43 +08:00
|
|
|
render json: {
|
2016-12-08 18:26:13 +08:00
|
|
|
html: render_to_string(partial: 'new.html.erb')
|
2016-02-12 23:52:43 +08:00
|
|
|
}, status: :ok
|
2016-12-08 18:26:13 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-06-07 19:29:39 +08:00
|
|
|
obj = create_multiple_results
|
2016-02-12 23:52:43 +08:00
|
|
|
respond_to do |format|
|
2017-06-07 19:29:39 +08:00
|
|
|
if obj.fetch(:status)
|
2017-03-13 20:51:31 +08:00
|
|
|
format.html do
|
2017-06-07 19:29:39 +08:00
|
|
|
flash[:success] = t('result_assets.create.success_flash',
|
|
|
|
module: @my_module.name)
|
2016-02-12 23:52:43 +08:00
|
|
|
redirect_to results_my_module_path(@my_module)
|
2017-03-13 20:51:31 +08:00
|
|
|
end
|
|
|
|
format.json do
|
2016-02-12 23:52:43 +08:00
|
|
|
render json: {
|
2017-03-13 20:51:31 +08:00
|
|
|
html: render_to_string(
|
2017-06-07 19:29:39 +08:00
|
|
|
partial: 'my_modules/results.html.erb',
|
|
|
|
locals: { results: obj.fetch(:results) }
|
2017-03-13 20:51:31 +08:00
|
|
|
)
|
2016-02-12 23:52:43 +08:00
|
|
|
}, status: :ok
|
2017-03-13 20:51:31 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
else
|
2017-06-07 19:29:39 +08:00
|
|
|
flash[:error] = t('result_assets.error_flash')
|
|
|
|
format.json do
|
|
|
|
render json: {}, status: :bad_request
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-13 20:51:31 +08:00
|
|
|
def edit
|
2016-02-12 23:52:43 +08:00
|
|
|
respond_to do |format|
|
2016-12-08 18:26:13 +08:00
|
|
|
format.json do
|
2016-02-12 23:52:43 +08:00
|
|
|
render json: {
|
2016-12-08 18:26:13 +08:00
|
|
|
html: render_to_string(partial: 'edit.html.erb')
|
2016-02-12 23:52:43 +08:00
|
|
|
}, status: :ok
|
2016-12-08 18:26:13 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
update_params = result_params
|
|
|
|
previous_size = @result.space_taken
|
|
|
|
|
2019-07-12 20:42:04 +08:00
|
|
|
@result.asset.last_modified_by = current_user if update_params.key? :asset_attributes
|
2016-08-05 23:00:29 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
@result.last_modified_by = current_user
|
|
|
|
@result.assign_attributes(update_params)
|
2017-10-19 17:38:23 +08:00
|
|
|
success_flash = t('result_assets.update.success_flash',
|
|
|
|
module: @my_module.name)
|
2016-08-05 23:00:29 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
if @result.archived_changed?(from: false, to: true)
|
2019-07-12 20:42:04 +08:00
|
|
|
if @result.asset.locked?
|
2016-10-03 22:49:41 +08:00
|
|
|
respond_to do |format|
|
2017-10-19 17:38:23 +08:00
|
|
|
format.html do
|
2016-10-03 22:49:41 +08:00
|
|
|
flash[:error] = t('result_assets.archive.error_flash')
|
|
|
|
redirect_to results_my_module_path(@my_module)
|
|
|
|
return
|
2017-10-19 17:38:23 +08:00
|
|
|
end
|
2016-10-03 22:49:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
saved = @result.archive(current_user)
|
2017-10-19 17:38:23 +08:00
|
|
|
success_flash = t('result_assets.archive.success_flash',
|
|
|
|
module: @my_module.name)
|
2016-02-12 23:52:43 +08:00
|
|
|
if saved
|
2019-03-14 02:05:29 +08:00
|
|
|
log_activity(:archive_result)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
elsif @result.archived_changed?(from: true, to: false)
|
|
|
|
render_403
|
|
|
|
else
|
2019-07-12 20:42:04 +08:00
|
|
|
if @result.asset.locked?
|
2016-10-03 19:43:35 +08:00
|
|
|
@result.errors.add(:asset_attributes,
|
2016-12-21 23:52:15 +08:00
|
|
|
t('result_assets.edit.locked_file_error'))
|
2016-10-03 19:43:35 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
status: 'error',
|
|
|
|
errors: @result.errors
|
|
|
|
}, status: :bad_request
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
# Asset (file) and/or name has been changed
|
|
|
|
saved = @result.save
|
|
|
|
|
2017-10-19 17:38:23 +08:00
|
|
|
if saved
|
2017-01-24 23:57:14 +08:00
|
|
|
# Release team's space taken due to
|
2016-02-12 23:52:43 +08:00
|
|
|
# previous asset being removed
|
2017-01-24 23:57:14 +08:00
|
|
|
team = @result.my_module.experiment.project.team
|
|
|
|
team.release_space(previous_size)
|
|
|
|
team.save
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Post process new file if neccesary
|
2017-10-19 17:38:23 +08:00
|
|
|
@result.asset.post_process_file(team) if @result.asset.present?
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-03-14 02:05:29 +08:00
|
|
|
log_activity(:edit_result)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if saved
|
2017-10-19 17:38:23 +08:00
|
|
|
format.html do
|
2016-02-12 23:52:43 +08:00
|
|
|
flash[:success] = success_flash
|
|
|
|
redirect_to results_my_module_path(@my_module)
|
2017-10-19 17:38:23 +08:00
|
|
|
end
|
|
|
|
format.json do
|
2016-02-12 23:52:43 +08:00
|
|
|
render json: {
|
2017-10-19 17:38:23 +08:00
|
|
|
html: render_to_string(
|
|
|
|
partial: 'my_modules/result.html.erb', locals: { result: @result }
|
|
|
|
)
|
2016-02-12 23:52:43 +08:00
|
|
|
}, status: :ok
|
2017-10-19 17:38:23 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
else
|
2017-10-19 17:38:23 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
status: :error,
|
|
|
|
errors: @result.errors
|
|
|
|
}, status: :bad_request
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_vars
|
|
|
|
@result_asset = ResultAsset.find_by_id(params[:id])
|
|
|
|
|
|
|
|
if @result_asset
|
|
|
|
@result = @result_asset.result
|
|
|
|
@my_module = @result.my_module
|
|
|
|
else
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_vars_nested
|
|
|
|
@my_module = MyModule.find_by_id(params[:my_module_id])
|
2017-10-19 17:38:23 +08:00
|
|
|
render_404 unless @my_module
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2018-02-09 23:14:40 +08:00
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_manage_module?(@my_module)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_archive_permissions
|
2018-02-19 21:47:36 +08:00
|
|
|
if result_params[:archived].to_s != '' && !can_manage_result?(@result)
|
2016-02-12 23:52:43 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def result_params
|
|
|
|
params.require(:result).permit(
|
|
|
|
:name, :archived,
|
|
|
|
asset_attributes: [
|
|
|
|
:id,
|
|
|
|
:file
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2017-06-07 19:29:39 +08:00
|
|
|
|
|
|
|
def create_multiple_results
|
|
|
|
success = true
|
|
|
|
results = []
|
2018-02-06 22:39:19 +08:00
|
|
|
params[:results_files].values.each_with_index do |file, index|
|
|
|
|
asset = Asset.new(file: file,
|
2017-06-07 19:29:39 +08:00
|
|
|
created_by: current_user,
|
|
|
|
last_modified_by: current_user,
|
|
|
|
team: current_team)
|
|
|
|
result = Result.new(user: current_user,
|
|
|
|
my_module: @my_module,
|
|
|
|
name: params[:results_names][index.to_s],
|
|
|
|
asset: asset,
|
|
|
|
last_modified_by: current_user)
|
|
|
|
if result.save && asset.save
|
|
|
|
results << result
|
|
|
|
# Post process file here
|
|
|
|
asset.post_process_file(@my_module.experiment.project.team)
|
|
|
|
|
2019-03-21 04:34:47 +08:00
|
|
|
log_activity(:add_result, result)
|
2017-06-07 19:29:39 +08:00
|
|
|
else
|
|
|
|
success = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
{ status: success, results: results }
|
|
|
|
end
|
2019-03-14 02:05:29 +08:00
|
|
|
|
2019-03-21 04:34:47 +08:00
|
|
|
def log_activity(type_of, result = @result)
|
2019-03-14 02:05:29 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
2019-03-21 04:34:47 +08:00
|
|
|
subject: result,
|
2019-03-14 02:05:29 +08:00
|
|
|
team: @my_module.experiment.project.team,
|
|
|
|
project: @my_module.experiment.project,
|
|
|
|
message_items: {
|
2019-03-21 04:34:47 +08:00
|
|
|
result: result.id,
|
2019-03-26 18:39:54 +08:00
|
|
|
type_of_result: t('activities.result_type.asset')
|
2019-03-14 02:05:29 +08:00
|
|
|
})
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|