mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-03 05:29:46 +08:00
Improve step reposition after deletion [SCI-4880]
This commit is contained in:
parent
3150a36a64
commit
dd347aa834
2 changed files with 12 additions and 21 deletions
|
@ -659,7 +659,7 @@ class Protocol < ApplicationRecord
|
|||
def destroy_contents
|
||||
# Calculate total space taken by the protocol
|
||||
st = space_taken
|
||||
steps.destroy_all
|
||||
steps.order(position: :desc).destroy_all
|
||||
|
||||
# Release space taken by the step
|
||||
team.release_space(st)
|
||||
|
|
|
@ -140,7 +140,7 @@ class Step < ApplicationRecord
|
|||
|
||||
def move_in_protocol(direction)
|
||||
transaction do
|
||||
adjust_duplicated_step_positions
|
||||
re_index_following_steps
|
||||
|
||||
case direction
|
||||
when :up
|
||||
|
@ -165,32 +165,23 @@ class Step < ApplicationRecord
|
|||
end
|
||||
|
||||
def adjust_positions_after_destroy
|
||||
adjust_duplicated_step_positions
|
||||
protocol.steps.where('position > ?', position).find_each do |step|
|
||||
re_index_following_steps
|
||||
protocol.steps.where('position > ?', position).order(:position).each do |step|
|
||||
step.update!(position: step.position - 1)
|
||||
end
|
||||
end
|
||||
|
||||
def adjust_duplicated_step_positions
|
||||
duplicated_steps = protocol.steps.where(position: position)
|
||||
return unless duplicated_steps.size > 1
|
||||
|
||||
# Shifting following steps
|
||||
next_steps = protocol.steps.where(position: (position + 1)..).order(:position)
|
||||
if next_steps.present?
|
||||
shift_by = duplicated_steps.size - next_steps.first.position + position
|
||||
if shift_by.positive?
|
||||
next_steps.reverse_each do |step|
|
||||
step.update!(position: step.position + shift_by)
|
||||
end
|
||||
end
|
||||
def re_index_following_steps
|
||||
steps = protocol.steps.where(position: position..).order(:position).where.not(id: id)
|
||||
i = position
|
||||
steps.each do |step|
|
||||
i += 1
|
||||
step.position = i
|
||||
end
|
||||
|
||||
# Re-index duplicated positions
|
||||
duplicated_steps.each_with_index do |step, i|
|
||||
step.update!(position: step.position + i)
|
||||
steps.reverse_each do |step|
|
||||
step.save! if step.position_changed?
|
||||
end
|
||||
reload
|
||||
end
|
||||
|
||||
def cascade_before_destroy
|
||||
|
|
Loading…
Reference in a new issue