From 8245ad97380be4da5667875738ea53f04de80d2a Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Wed, 30 Aug 2023 14:54:38 +0200 Subject: [PATCH] Fix up result element duplicate endpoints [SCI-9107] --- .../result_elements/base_controller.rb | 2 +- .../result_elements/tables_controller.rb | 26 ++++++++++--------- .../result_elements/texts_controller.rb | 24 ++++++++--------- app/models/table.rb | 2 +- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/app/controllers/result_elements/base_controller.rb b/app/controllers/result_elements/base_controller.rb index e3b72ef13..3abfbeec3 100644 --- a/app/controllers/result_elements/base_controller.rb +++ b/app/controllers/result_elements/base_controller.rb @@ -38,7 +38,7 @@ module ResultElements render json: result_orderable_element, serializer: ResultOrderableElementSerializer, user: current_user end - def log_step_activity(element_type_of, message_items) + def log_result_activity(element_type_of, message_items) # TODO #message_items[:my_module] = @protocol.my_module.id if @protocol.in_module? #Activities::CreateActivityService.call( diff --git a/app/controllers/result_elements/tables_controller.rb b/app/controllers/result_elements/tables_controller.rb index 691b284af..344f4d79b 100644 --- a/app/controllers/result_elements/tables_controller.rb +++ b/app/controllers/result_elements/tables_controller.rb @@ -72,7 +72,7 @@ module ResultElements def destroy if @table.destroy - #log_step_activity(:table_deleted, { table_name: @table.name }) + log_result_activity(:table_deleted, { table_name: @table.name }) head :ok else head :unprocessable_entity @@ -80,17 +80,19 @@ module ResultElements end 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 - # @table.name += ' (1)' - # new_table = @table.duplicate(@step, current_user, position + 1) - # log_step_activity(:table_duplicated, { table_name: new_table.name }) - # render_step_orderable_element(new_table.step_table) - #end - rescue ActiveRecord::RecordInvalid + ActiveRecord::Base.transaction do + position = @table.result_table.result_orderable_element.position + @result.result_orderable_elements.where('position > ?', position).order(position: :desc).each do |element| + element.update(position: element.position + 1) + end + @table.name += ' (1)' + new_table = @table.duplicate(@result, current_user, position + 1) + # log_result_activity(:table_duplicated, { table_name: new_table.name }) + render_result_orderable_element(new_table.result_table) + end + rescue ActiveRecord::RecordInvalid => e + Rails.logger.error(e.message) + Rails.logger.error(e.backtrace.join("\n")) head :unprocessable_entity end diff --git a/app/controllers/result_elements/texts_controller.rb b/app/controllers/result_elements/texts_controller.rb index 5e3b54f87..76d5c2ebb 100644 --- a/app/controllers/result_elements/texts_controller.rb +++ b/app/controllers/result_elements/texts_controller.rb @@ -14,7 +14,7 @@ module ResultElements ActiveRecord::Base.transaction do create_in_result!(@result, result_text) - #log_step_activity(:text_added, { text_name: step_text.name }) + #log_result_activity(:text_added, { text_name: result_text.name }) end render_result_orderable_element(result_text) @@ -27,7 +27,7 @@ module ResultElements ActiveRecord::Base.transaction do @result_text.update!(result_text_params) TinyMceAsset.update_images(@result_text, params[:tiny_mce_images], current_user) - #log_step_activity(:text_edited, { text_name: @step_text.name }) + #log_result_activity(:text_edited, { text_name: @result_text.name }) result_annotation_notification(old_text) end @@ -50,7 +50,7 @@ module ResultElements def destroy if @result_text.destroy - log_step_activity(:text_deleted, { text_name: @result_text.name }) + log_result_activity(:text_deleted, { text_name: @result_text.name }) head :ok else head :unprocessable_entity @@ -58,15 +58,15 @@ module ResultElements end def duplicate - #ActiveRecord::Base.transaction do - # position = @step_text.step_orderable_element.position - # @step.step_orderable_elements.where('position > ?', position).order(position: :desc).each do |element| - # element.update(position: element.position + 1) - # end - # new_step_text = @step_text.duplicate(@step, position + 1) - # log_step_activity(:text_duplicated, { text_name: new_step_text.name }) - # render_step_orderable_element(new_step_text) - #end + 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) + # log_result_activity(:text_duplicated, { text_name: new_result_text.name }) + render_result_orderable_element(new_result_text) + end rescue ActiveRecord::RecordInvalid head :unprocessable_entity end diff --git a/app/models/table.rb b/app/models/table.rb index b6b55be33..9930c30cf 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -166,7 +166,7 @@ class Table < ApplicationRecord ) parent.result_orderable_elements.create!( - position: parent.result_orderable_elements.length, + position: position || parent.result_orderable_elements.length, orderable: new_table.result_table )