2023-08-10 22:48:08 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ResultElements
|
|
|
|
class TextsController < BaseController
|
|
|
|
include ActionView::Helpers::UrlHelper
|
|
|
|
include ApplicationHelper
|
|
|
|
include InputSanitizeHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
2023-08-21 15:22:56 +08:00
|
|
|
before_action :load_result_text, only: %i(update destroy duplicate move)
|
2023-08-10 22:48:08 +08:00
|
|
|
|
|
|
|
def create
|
|
|
|
result_text = @result.result_texts.build
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2023-08-11 20:48:20 +08:00
|
|
|
create_in_result!(@result, result_text)
|
2023-09-07 22:34:33 +08:00
|
|
|
log_result_activity(:result_text_added, { text_name: result_text.name })
|
2023-08-10 22:48:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render_result_orderable_element(result_text)
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
old_text = @result_text.text
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@result_text.update!(result_text_params)
|
|
|
|
TinyMceAsset.update_images(@result_text, params[:tiny_mce_images], current_user)
|
2023-09-07 22:34:33 +08:00
|
|
|
log_result_activity(:result_text_edited, { text_name: @result_text.name })
|
2023-08-10 22:48:08 +08:00
|
|
|
result_annotation_notification(old_text)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: @result_text, serializer: ResultTextSerializer, user: current_user
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
render json: @result_text.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2023-08-21 15:22:56 +08:00
|
|
|
def move
|
2023-09-29 20:32:33 +08:00
|
|
|
target = @my_module.results.active.find_by(id: params[:target_id])
|
|
|
|
return head(:conflict) unless target
|
|
|
|
|
2023-08-21 15:22:56 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@result_text.update!(result: target)
|
2023-10-04 17:24:06 +08:00
|
|
|
@result_text.result_orderable_element.destroy
|
|
|
|
new_orderable_element = target.result_orderable_elements.build(orderable: @result_text)
|
|
|
|
new_orderable_element.insert_at(target.result_orderable_elements.count)
|
2023-08-21 15:22:56 +08:00
|
|
|
@result.normalize_elements_position
|
|
|
|
render json: @result_text, serializer: ResultTextSerializer, user: current_user
|
2023-09-19 07:17:30 +08:00
|
|
|
|
|
|
|
log_result_activity(
|
|
|
|
:result_text_moved,
|
|
|
|
{
|
|
|
|
user: current_user.id,
|
|
|
|
text_name: @result_text.name,
|
|
|
|
result_original: @result.id,
|
|
|
|
result_destination: target.id
|
|
|
|
}
|
|
|
|
)
|
2023-08-21 15:22:56 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
render json: @result_text.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-10 22:48:08 +08:00
|
|
|
def destroy
|
|
|
|
if @result_text.destroy
|
2023-09-07 22:34:33 +08:00
|
|
|
log_result_activity(:result_text_deleted, { text_name: @result_text.name })
|
2023-08-10 22:48:08 +08:00
|
|
|
head :ok
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def duplicate
|
2023-08-30 20:54:38 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
position = @result_text.result_orderable_element.position
|
|
|
|
@result.result_orderable_elements.where('position > ?', position).order(position: :desc).each do |element|
|
|
|
|
element.update(position: element.position + 1)
|
|
|
|
end
|
|
|
|
new_result_text = @result_text.duplicate(@result, position + 1)
|
2023-09-07 22:34:33 +08:00
|
|
|
log_result_activity(:result_text_duplicated, { text_name: new_result_text.name })
|
2023-08-30 20:54:38 +08:00
|
|
|
render_result_orderable_element(new_result_text)
|
|
|
|
end
|
2023-08-10 22:48:08 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def result_text_params
|
2023-09-04 19:58:11 +08:00
|
|
|
params.require(:text_component).permit(:text, :name)
|
2023-08-10 22:48:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_result_text
|
|
|
|
@result_text = @result.result_texts.find_by(id: params[:id])
|
|
|
|
return render_404 unless @result_text
|
|
|
|
end
|
|
|
|
|
|
|
|
def result_annotation_notification(old_text = nil)
|
|
|
|
smart_annotation_notification(
|
|
|
|
old_text: (old_text if old_text),
|
|
|
|
new_text: @result_text.text,
|
2023-11-27 18:29:27 +08:00
|
|
|
subject: @result,
|
2023-08-10 22:48:08 +08:00
|
|
|
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)),
|
|
|
|
experiment: link_to(@result.my_module.experiment.name,
|
|
|
|
my_modules_experiment_url(@result.my_module
|
|
|
|
.experiment)),
|
|
|
|
my_module: link_to(@result.my_module.name,
|
|
|
|
protocols_my_module_url(
|
|
|
|
@result.my_module
|
|
|
|
)))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|