2022-05-25 20:23:35 +08:00
|
|
|
# frozen_string_literal: true
|
2022-04-29 18:29:42 +08:00
|
|
|
|
2022-05-30 19:45:51 +08:00
|
|
|
module StepElements
|
2022-05-25 20:23:35 +08:00
|
|
|
class TablesController < BaseController
|
2022-08-22 20:02:19 +08:00
|
|
|
before_action :load_table, only: %i(update destroy duplicate)
|
2022-04-29 18:29:42 +08:00
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
def create
|
2023-04-19 17:45:32 +08:00
|
|
|
predefined_table_dimensions = create_table_params[:tableDimensions].map(&:to_i)
|
2023-04-18 21:42:53 +08:00
|
|
|
name = if predefined_table_dimensions[0] == predefined_table_dimensions[1]
|
|
|
|
t('protocols.steps.table.default_name',
|
|
|
|
position: @step.step_tables.length + 1)
|
|
|
|
else
|
|
|
|
t('protocols.steps.plate.default_name',
|
|
|
|
position: @step.step_tables.length + 1)
|
|
|
|
end
|
2022-05-25 20:23:35 +08:00
|
|
|
step_table = @step.step_tables.new(table:
|
|
|
|
Table.new(
|
2023-04-18 21:42:53 +08:00
|
|
|
name: name,
|
|
|
|
contents: { data: Array.new(predefined_table_dimensions[0],
|
|
|
|
Array.new(predefined_table_dimensions[1], '')) }.to_json,
|
2023-05-08 16:23:44 +08:00
|
|
|
metadata: { plateTemplate: create_table_params[:plateTemplate] == 'true' },
|
2023-03-07 18:26:57 +08:00
|
|
|
created_by: current_user,
|
|
|
|
team: @step.protocol.team
|
2022-04-29 18:29:42 +08:00
|
|
|
))
|
2022-05-25 20:23:35 +08:00
|
|
|
|
2022-06-02 17:15:32 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
create_in_step!(@step, step_table)
|
|
|
|
log_step_activity(:table_added, { table_name: step_table.table.name })
|
|
|
|
end
|
2022-05-25 20:23:35 +08:00
|
|
|
|
|
|
|
render_step_orderable_element(step_table)
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2022-06-02 17:15:32 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
2023-03-28 19:29:02 +08:00
|
|
|
@table.assign_attributes(table_params.except(:metadata))
|
|
|
|
begin
|
2023-04-19 17:45:32 +08:00
|
|
|
if table_params[:metadata].present?
|
2023-05-26 17:12:47 +08:00
|
|
|
|
|
|
|
@table.metadata = if @table.metadata
|
|
|
|
@table.metadata.merge(JSON.parse(table_params[:metadata]))
|
|
|
|
else
|
|
|
|
JSON.parse(table_params[:metadata])
|
|
|
|
end
|
2023-04-19 17:45:32 +08:00
|
|
|
end
|
2023-03-28 19:29:02 +08:00
|
|
|
rescue JSON::ParserError
|
|
|
|
@table.metadata = {}
|
|
|
|
end
|
|
|
|
@table.save!
|
2022-06-02 17:15:32 +08:00
|
|
|
log_step_activity(:table_edited, { table_name: @table.name })
|
|
|
|
end
|
|
|
|
|
2022-06-07 18:10:10 +08:00
|
|
|
render json: @table, serializer: TableSerializer, user: current_user
|
2022-05-25 20:23:35 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
if @table.destroy
|
2022-06-02 17:15:32 +08:00
|
|
|
log_step_activity(:table_deleted, { table_name: @table.name })
|
2022-05-25 20:23:35 +08:00
|
|
|
head :ok
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|
|
|
|
|
2022-08-22 20:02:19 +08:00
|
|
|
def duplicate
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
position = @table.step_table.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
|
|
|
@table.name += ' (1)'
|
2022-08-22 20:02:19 +08:00
|
|
|
new_table = @table.duplicate(@step, current_user, position + 1)
|
2022-08-30 21:26:52 +08:00
|
|
|
log_step_activity(:table_duplicated, { table_name: new_table.name })
|
2022-08-22 20:02:19 +08:00
|
|
|
render_step_orderable_element(new_table.step_table)
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
def table_params
|
2023-03-28 19:29:02 +08:00
|
|
|
params.permit(:name, :contents, :metadata)
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|
2022-05-03 21:15:49 +08:00
|
|
|
|
2023-04-19 17:45:32 +08:00
|
|
|
def create_table_params
|
|
|
|
params.permit(:plateTemplate, tableDimensions: [])
|
|
|
|
end
|
|
|
|
|
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
|