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
|
|
|
|
before_action :load_checklist, only: %i(update destroy)
|
2022-04-29 18:29:42 +08:00
|
|
|
|
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 })
|
|
|
|
end
|
2022-05-25 20:23:35 +08:00
|
|
|
render_step_orderable_element(checklist)
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
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 })
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|