2016-02-12 23:52:43 +08:00
|
|
|
class ResultTextsController < ApplicationController
|
|
|
|
include ResultsHelper
|
2017-04-06 15:33:49 +08:00
|
|
|
include ActionView::Helpers::UrlHelper
|
|
|
|
include ApplicationHelper
|
2017-05-25 14:57:50 +08:00
|
|
|
include InputSanitizeHelper
|
2017-04-06 15:33:49 +08:00
|
|
|
include Rails.application.routes.url_helpers
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
before_action :load_vars, only: [:edit, :update, :download]
|
|
|
|
before_action :load_vars_nested, only: [:new, :create]
|
|
|
|
|
2021-09-24 19:11:41 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(edit update)
|
|
|
|
before_action :check_create_permissions, only: %i(new create)
|
2016-02-12 23:52:43 +08:00
|
|
|
before_action :check_archive_permissions, only: [:update]
|
2021-09-24 19:11:41 +08:00
|
|
|
before_action :check_view_permissions, except: %i(new create edit update)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
def new
|
|
|
|
@result = Result.new(
|
|
|
|
user: current_user,
|
|
|
|
my_module: @my_module
|
|
|
|
)
|
|
|
|
@result.build_result_text
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json {
|
|
|
|
render json: {
|
2023-07-03 17:11:51 +08:00
|
|
|
html: render_to_string({ partial: 'new', formats: :html })
|
2016-02-12 23:52:43 +08:00
|
|
|
}, status: :ok
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@result_text = ResultText.new(result_params[:result_text_attributes])
|
|
|
|
@result = Result.new(
|
|
|
|
user: current_user,
|
|
|
|
my_module: @my_module,
|
|
|
|
name: result_params[:name],
|
|
|
|
result_text: @result_text
|
|
|
|
)
|
|
|
|
@result.last_modified_by = current_user
|
|
|
|
|
2020-07-16 21:42:52 +08:00
|
|
|
if @result.save && @result_text.save
|
|
|
|
# link tiny_mce_assets to the text result
|
|
|
|
TinyMceAsset.update_images(@result_text, params[:tiny_mce_images], current_user)
|
|
|
|
|
|
|
|
result_annotation_notification
|
|
|
|
log_activity(:add_result)
|
|
|
|
flash[:success] = t('result_texts.create.success_flash', module: @my_module.name)
|
2020-07-28 21:39:29 +08:00
|
|
|
redirect_to results_my_module_path(@my_module, page: params[:page], order: params[:order])
|
2020-07-16 21:42:52 +08:00
|
|
|
else
|
|
|
|
render json: @result.errors, status: :bad_request
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
respond_to do |format|
|
|
|
|
format.json {
|
|
|
|
render json: {
|
|
|
|
html: render_to_string({
|
|
|
|
partial: "edit.html.erb"
|
|
|
|
})
|
|
|
|
}, status: :ok
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2017-04-06 15:33:49 +08:00
|
|
|
old_text = @result_text.text
|
2016-02-12 23:52:43 +08:00
|
|
|
update_params = result_params
|
|
|
|
@result.last_modified_by = current_user
|
|
|
|
@result.assign_attributes(update_params)
|
2019-03-11 20:43:50 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
success_flash = t("result_texts.update.success_flash",
|
|
|
|
module: @my_module.name)
|
|
|
|
if @result.archived_changed?(from: false, to: true)
|
|
|
|
saved = @result.archive(current_user)
|
|
|
|
success_flash = t("result_texts.archive.success_flash",
|
|
|
|
module: @my_module.name)
|
|
|
|
if saved
|
2019-04-23 16:31:37 +08:00
|
|
|
|
2019-03-14 02:05:29 +08:00
|
|
|
log_activity(:archive_result)
|
2019-04-23 16:31:37 +08:00
|
|
|
|
2019-08-09 15:47:07 +08:00
|
|
|
TinyMceAsset.update_images(@result_text, params[:tiny_mce_images], current_user)
|
2019-04-23 16:31:37 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
elsif @result.archived_changed?(from: true, to: false)
|
|
|
|
render_403
|
|
|
|
else
|
|
|
|
saved = @result.save
|
|
|
|
|
|
|
|
if saved then
|
2019-04-23 16:31:37 +08:00
|
|
|
|
2019-03-14 02:05:29 +08:00
|
|
|
log_activity(:edit_result)
|
2019-04-23 16:31:37 +08:00
|
|
|
|
2019-08-09 15:47:07 +08:00
|
|
|
TinyMceAsset.update_images(@result_text, params[:tiny_mce_images], current_user)
|
2019-04-23 16:31:37 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
2017-04-06 15:33:49 +08:00
|
|
|
|
|
|
|
result_annotation_notification(old_text) if saved
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
respond_to do |format|
|
|
|
|
if saved
|
2023-07-05 18:43: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)
|
2023-07-05 18:43:23 +08:00
|
|
|
end
|
|
|
|
format.json do
|
2016-02-12 23:52:43 +08:00
|
|
|
render json: {
|
2023-07-05 18:43:23 +08:00
|
|
|
html: render_to_string(
|
|
|
|
partial: 'my_modules/result.html.erb',
|
|
|
|
locals: { result: @result },
|
|
|
|
formats: :html
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: @result.errors, status: :bad_request
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def download
|
|
|
|
send_data @result_text.text, filename: @result_text.result.name + '.txt',
|
|
|
|
type: 'plain/text'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_vars
|
|
|
|
@result_text = ResultText.find_by_id(params[:id])
|
|
|
|
|
|
|
|
if @result_text
|
|
|
|
@result = @result_text.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
|
|
|
|
|
2021-09-24 19:11:41 +08:00
|
|
|
def check_create_permissions
|
|
|
|
render_403 unless can_create_results?(@my_module)
|
|
|
|
end
|
|
|
|
|
2018-02-09 23:14:40 +08:00
|
|
|
def check_manage_permissions
|
2021-09-24 19:11:41 +08:00
|
|
|
render_403 unless can_manage_result?(@result)
|
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
|
|
|
|
|
2021-09-24 19:11:41 +08:00
|
|
|
def check_view_permissions
|
|
|
|
render_403 unless can_read_result?(@result)
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def result_params
|
|
|
|
params.require(:result).permit(
|
|
|
|
:name, :archived,
|
|
|
|
result_text_attributes: [
|
|
|
|
:id,
|
|
|
|
:text
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-04-06 15:33:49 +08:00
|
|
|
def result_annotation_notification(old_text = nil)
|
|
|
|
smart_annotation_notification(
|
|
|
|
old_text: (old_text if old_text),
|
|
|
|
new_text: @result_text.text,
|
|
|
|
title: t('notifications.result_annotation_title',
|
|
|
|
result: @result.name,
|
|
|
|
user: current_user.full_name),
|
|
|
|
message: t('notifications.result_annotation_message_html',
|
|
|
|
project: link_to(@result.my_module.experiment.project.name,
|
|
|
|
project_url(@result.my_module
|
|
|
|
.experiment
|
|
|
|
.project)),
|
2017-04-20 19:05:49 +08:00
|
|
|
experiment: link_to(@result.my_module.experiment.name,
|
2023-01-12 18:31:04 +08:00
|
|
|
my_modules_experiment_url(@result.my_module
|
|
|
|
.experiment)),
|
2017-04-06 15:33:49 +08:00
|
|
|
my_module: link_to(@result.my_module.name,
|
|
|
|
protocols_my_module_url(
|
|
|
|
@result.my_module
|
|
|
|
)))
|
|
|
|
)
|
|
|
|
end
|
2019-03-14 02:05:29 +08:00
|
|
|
|
|
|
|
def log_activity(type_of)
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @result,
|
2022-11-24 22:19:17 +08:00
|
|
|
team: @my_module.team,
|
|
|
|
project: @my_module.project,
|
2019-03-14 02:05:29 +08:00
|
|
|
message_items: {
|
|
|
|
result: @result.id,
|
2019-03-26 18:39:54 +08:00
|
|
|
type_of_result: t('activities.result_type.text')
|
2019-03-14 02:05:29 +08:00
|
|
|
})
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|