scinote-web/app/controllers/result_assets_controller.rb

263 lines
6.9 KiB
Ruby
Raw Normal View History

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]
before_action :check_create_permissions, only: [:new, :create]
before_action :check_edit_permissions, only: [:edit, :update]
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
obj = create_multiple_results
2016-02-12 23:52:43 +08:00
respond_to do |format|
if obj.fetch(:status)
2017-03-13 20:51:31 +08:00
format.html do
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(
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
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
2016-10-03 19:43:35 +08:00
previous_asset = @result.asset
2016-02-12 23:52:43 +08:00
if update_params.key? :asset_attributes
asset = Asset.find_by_id(update_params[:asset_attributes][:id])
asset.created_by = current_user
asset.last_modified_by = current_user
asset.team = current_team
@result.asset = asset
end
2016-02-12 23:52:43 +08:00
@result.last_modified_by = current_user
@result.assign_attributes(update_params)
success_flash = t("result_assets.update.success_flash",
module: @my_module.name)
2016-02-12 23:52:43 +08:00
if @result.archived_changed?(from: false, to: true)
2016-10-03 22:49:41 +08:00
if previous_asset.locked?
respond_to do |format|
format.html {
flash[:error] = t('result_assets.archive.error_flash')
redirect_to results_my_module_path(@my_module)
return
}
end
end
2016-02-12 23:52:43 +08:00
saved = @result.archive(current_user)
success_flash = t("result_assets.archive.success_flash",
module: @my_module.name)
if saved
Activity.create(
type_of: :archive_result,
2016-08-10 19:43:27 +08:00
project: @my_module.experiment.project,
2017-04-20 16:56:25 +08:00
experiment: @my_module.experiment,
2016-02-12 23:52:43 +08:00
my_module: @my_module,
user: current_user,
message: t(
'activities.archive_asset_result',
user: current_user.full_name,
result: @result.name
)
)
end
elsif @result.archived_changed?(from: true, to: false)
render_403
else
2016-10-03 19:43:35 +08:00
if previous_asset.locked?
@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
if saved then
# Release team's space taken due to
2016-02-12 23:52:43 +08:00
# previous asset being removed
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
if @result.asset.present?
@result.asset.post_process_file(team)
2016-02-12 23:52:43 +08:00
end
Activity.create(
type_of: :edit_result,
user: current_user,
2016-08-10 19:43:27 +08:00
project: @my_module.experiment.project,
2017-04-20 16:56:25 +08:00
experiment: @my_module.experiment,
2016-02-12 23:52:43 +08:00
my_module: @my_module,
message: t(
"activities.edit_asset_result",
user: current_user.full_name,
result: @result.name
)
)
end
end
respond_to do |format|
if saved
format.html {
flash[:success] = success_flash
redirect_to results_my_module_path(@my_module)
}
format.json {
render json: {
html: render_to_string({
partial: "my_modules/result.html.erb", locals: {result: @result}
})
}, status: :ok
}
else
format.json {
render json: @result.errors, status: :bad_request
}
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])
unless @my_module
render_404
end
end
def check_create_permissions
unless can_create_result_asset_in_module(@my_module)
render_403
end
end
def check_edit_permissions
unless can_edit_result_asset_in_module(@my_module)
render_403
end
end
def check_archive_permissions
if result_params[:archived].to_s != '' and
not can_archive_result(@result)
render_403
end
end
def result_params
params.require(:result).permit(
:name, :archived,
asset_attributes: [
:id,
:file
]
)
end
def create_multiple_results
success = true
results = []
params[:results_files].each_with_index do |file, index|
asset = Asset.new(file: file.second,
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)
# Generate activity
Activity.create(
type_of: :add_result,
user: current_user,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
message: t('activities.add_asset_result',
user: current_user.full_name,
result: result.name)
)
else
success = false
end
end
{ status: success, results: results }
end
2016-02-12 23:52:43 +08:00
end