diff --git a/app/controllers/result_orderable_elements_controller.rb b/app/controllers/result_orderable_elements_controller.rb index c129836cb..a1b98162b 100644 --- a/app/controllers/result_orderable_elements_controller.rb +++ b/app/controllers/result_orderable_elements_controller.rb @@ -5,15 +5,18 @@ class ResultOrderableElementsController < ApplicationController before_action :check_manage_permissions def reorder + position_changed = false ActiveRecord::Base.transaction do params[:result_orderable_element_positions].each do |id, position| result_element = @result.result_orderable_elements.find(id) - result_element.insert_at(position) + position_changed ||= result_element.insert_at(position) end end - log_activity(:result_content_rearranged, @my_module.experiment.project, my_module: @my_module.id) - @result.touch + if position_changed + log_activity(:result_content_rearranged, @my_module.experiment.project, my_module: @my_module.id) + @result.touch + end render json: params[:result_orderable_element_positions], status: :ok rescue ActiveRecord::RecordInvalid diff --git a/app/controllers/step_orderable_elements_controller.rb b/app/controllers/step_orderable_elements_controller.rb index 801c6fbac..d4e322dd9 100644 --- a/app/controllers/step_orderable_elements_controller.rb +++ b/app/controllers/step_orderable_elements_controller.rb @@ -6,16 +6,23 @@ class StepOrderableElementsController < ApplicationController def reorder @step.with_lock do + position_changed = false params[:step_orderable_element_positions].each do |id, position| - @step.step_orderable_elements.find(id).update_column(:position, position) + step_element = @step.step_orderable_elements.find(id) + if step_element.position != position + position_changed = true + step_element.update_column(:position, position) + end end - if @protocol.in_module? - log_activity(:task_step_content_rearranged, @my_module.experiment.project, my_module: @my_module.id) - else - log_activity(:protocol_step_content_rearranged, nil, protocol: @protocol.id) + if position_changed + if @protocol.in_module? + log_activity(:task_step_content_rearranged, @my_module.experiment.project, my_module: @my_module.id) + else + log_activity(:protocol_step_content_rearranged, nil, protocol: @protocol.id) + end + @step.touch end - @step.touch end render json: params[:step_orderable_element_positions], status: :ok diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index c2b1c0764..40f2845f5 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -241,16 +241,23 @@ class StepsController < ApplicationController def reorder @protocol.with_lock do + position_changed = false params[:step_positions].each do |id, position| - @protocol.steps.find(id).update_column(:position, position) + step = @protocol.steps.find(id) + if position != step.position + position_changed = true + step.update_column(:position, position) + end end - if @protocol.in_module? - log_activity(:task_steps_rearranged, @my_module.experiment.project, my_module: @my_module.id) - else - log_activity(:protocol_steps_rearranged, nil, protocol: @protocol.id) + if position_changed + if @protocol.in_module? + log_activity(:task_steps_rearranged, @my_module.experiment.project, my_module: @my_module.id) + else + log_activity(:protocol_steps_rearranged, nil, protocol: @protocol.id) + end + @protocol.touch end - @protocol.touch end render json: {