2022-04-29 18:29:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-05-30 19:45:51 +08:00
|
|
|
module StepElements
|
2022-05-25 20:23:35 +08:00
|
|
|
class ChecklistsController < BaseController
|
2022-12-01 21:55:23 +08:00
|
|
|
include ApplicationHelper
|
|
|
|
include StepsActions
|
2023-08-21 15:22:56 +08:00
|
|
|
before_action :load_checklist, only: %i(update destroy duplicate move)
|
2022-05-25 20:23:35 +08:00
|
|
|
def create
|
|
|
|
checklist = @step.checklists.build(
|
2022-05-05 18:56:31 +08:00
|
|
|
name: t('protocols.steps.checklist.default_name', position: @step.checklists.length + 1)
|
2022-04-29 18:29:42 +08:00
|
|
|
)
|
2022-06-02 17:15:32 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
create_in_step!(@step, checklist)
|
|
|
|
log_step_activity(:checklist_added, { checklist_name: checklist.name })
|
2022-12-01 21:55:23 +08:00
|
|
|
checklist_name_annotation(@step, checklist)
|
2022-06-02 17:15:32 +08:00
|
|
|
end
|
2022-05-25 20:23:35 +08:00
|
|
|
render_step_orderable_element(checklist)
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2022-12-01 21:55:23 +08:00
|
|
|
old_name = @checklist.name
|
2022-06-02 17:15:32 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@checklist.update!(checklist_params)
|
|
|
|
log_step_activity(:checklist_edited, { checklist_name: @checklist.name })
|
2022-12-01 21:55:23 +08:00
|
|
|
checklist_name_annotation(@step, @checklist, old_name)
|
2022-06-02 17:15:32 +08:00
|
|
|
end
|
|
|
|
|
2022-06-07 18:10:10 +08:00
|
|
|
render json: @checklist, serializer: ChecklistSerializer, user: current_user
|
2022-05-25 20:23:35 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|
|
|
|
|
2023-08-21 15:22:56 +08:00
|
|
|
def move
|
|
|
|
target = @protocol.steps.find_by(id: params[:target_id])
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@checklist.update!(step: target)
|
|
|
|
@checklist.step_orderable_element.update!(step: target, position: target.step_orderable_elements.size)
|
|
|
|
@step.normalize_elements_position
|
2023-09-19 07:17:30 +08:00
|
|
|
|
|
|
|
log_step_activity(
|
|
|
|
:checklist_moved,
|
|
|
|
{
|
|
|
|
user: current_user.id,
|
|
|
|
checklist_name: @checklist.name,
|
|
|
|
step_position_original: @step.position + 1,
|
|
|
|
step_original: @step.id,
|
|
|
|
step_position_destination: target.position + 1,
|
|
|
|
step_destination: target.id
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-08-21 15:22:56 +08:00
|
|
|
render json: @checklist, serializer: ChecklistSerializer, user: current_user
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
render json: @checklist.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
def destroy
|
|
|
|
if @checklist.destroy
|
2022-06-02 17:15:32 +08:00
|
|
|
log_step_activity(:checklist_deleted, { checklist_name: @checklist.name })
|
2022-05-25 20:23:35 +08:00
|
|
|
head :ok
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-22 20:02:19 +08:00
|
|
|
def duplicate
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
position = @checklist.step_orderable_element.position
|
|
|
|
@step.step_orderable_elements.where('position > ?', position).order(position: :desc).each do |element|
|
|
|
|
element.update(position: element.position + 1)
|
|
|
|
end
|
2022-11-29 19:29:13 +08:00
|
|
|
@checklist.name += ' (1)'
|
2022-08-22 20:02:19 +08:00
|
|
|
new_checklist = @checklist.duplicate(@step, current_user, position + 1)
|
2022-08-30 21:26:52 +08:00
|
|
|
log_step_activity(:checklist_duplicated, { checklist_name: @checklist.name })
|
2022-08-22 20:02:19 +08:00
|
|
|
render_step_orderable_element(new_checklist)
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def checklist_params
|
|
|
|
params.permit(:name, :text)
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|
2022-05-03 21:15:49 +08:00
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
def load_checklist
|
|
|
|
@checklist = @step.checklists.find_by(id: params[:id])
|
|
|
|
return render_404 unless @checklist
|
2022-05-03 21:15:49 +08:00
|
|
|
end
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|
|
|
|
end
|