2022-05-25 20:23:35 +08:00
|
|
|
# frozen_string_literal: true
|
2022-04-29 18:29:42 +08:00
|
|
|
|
|
|
|
module StepComponents
|
2022-05-25 20:23:35 +08:00
|
|
|
class TablesController < BaseController
|
|
|
|
before_action :load_table, only: %i(update destroy)
|
2022-04-29 18:29:42 +08:00
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
def create
|
|
|
|
step_table = @step.step_tables.new(table:
|
|
|
|
Table.new(
|
2022-04-29 18:29:42 +08:00
|
|
|
name: t('protocols.steps.table.default_name', position: @step.step_tables.length + 1),
|
2022-05-06 17:59:22 +08:00
|
|
|
contents: { data: Array.new(5, Array.new(5, '')) }.to_json,
|
2022-04-29 18:29:42 +08:00
|
|
|
created_by: current_user
|
|
|
|
))
|
2022-05-25 20:23:35 +08:00
|
|
|
|
|
|
|
create_in_step!(@step, step_table)
|
|
|
|
|
|
|
|
render_step_orderable_element(step_table)
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@table.update!(table_params)
|
|
|
|
render json: @table, serializer: TableSerializer
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
if @table.destroy
|
|
|
|
head :ok
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
def table_params
|
2022-05-06 17:59:22 +08:00
|
|
|
params.permit(:name, :contents)
|
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_table
|
|
|
|
@table = @step.tables.find_by(id: params[:id])
|
|
|
|
return render_404 unless @table
|
2022-05-03 21:15:49 +08:00
|
|
|
end
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|
|
|
|
end
|