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
|
def destroy_contents
|
||||||
# Calculate total space taken by the protocol
|
# Calculate total space taken by the protocol
|
||||||
st = space_taken
|
st = space_taken
|
||||||
steps.destroy_all
|
steps.order(position: :desc).destroy_all
|
||||||
|
|
||||||
# Release space taken by the step
|
# Release space taken by the step
|
||||||
team.release_space(st)
|
team.release_space(st)
|
||||||
|
|
|
@ -140,7 +140,7 @@ class Step < ApplicationRecord
|
||||||
|
|
||||||
def move_in_protocol(direction)
|
def move_in_protocol(direction)
|
||||||
transaction do
|
transaction do
|
||||||
adjust_duplicated_step_positions
|
re_index_following_steps
|
||||||
|
|
||||||
case direction
|
case direction
|
||||||
when :up
|
when :up
|
||||||
|
@ -165,32 +165,23 @@ class Step < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def adjust_positions_after_destroy
|
def adjust_positions_after_destroy
|
||||||
adjust_duplicated_step_positions
|
re_index_following_steps
|
||||||
protocol.steps.where('position > ?', position).find_each do |step|
|
protocol.steps.where('position > ?', position).order(:position).each do |step|
|
||||||
step.update!(position: step.position - 1)
|
step.update!(position: step.position - 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def adjust_duplicated_step_positions
|
def re_index_following_steps
|
||||||
duplicated_steps = protocol.steps.where(position: position)
|
steps = protocol.steps.where(position: position..).order(:position).where.not(id: id)
|
||||||
return unless duplicated_steps.size > 1
|
i = position
|
||||||
|
steps.each do |step|
|
||||||
# Shifting following steps
|
i += 1
|
||||||
next_steps = protocol.steps.where(position: (position + 1)..).order(:position)
|
step.position = i
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Re-index duplicated positions
|
steps.reverse_each do |step|
|
||||||
duplicated_steps.each_with_index do |step, i|
|
step.save! if step.position_changed?
|
||||||
step.update!(position: step.position + i)
|
|
||||||
end
|
end
|
||||||
reload
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def cascade_before_destroy
|
def cascade_before_destroy
|
||||||
|
|
Loading…
Reference in a new issue