You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.0 KiB
35 lines
1.0 KiB
5 years ago
|
class Admins::DragPortalImageService < ApplicationService
|
||
|
Error = Class.new(StandardError)
|
||
|
|
||
|
attr_reader :move, :after
|
||
|
|
||
|
def initialize(move, after)
|
||
|
@move = move
|
||
|
@after = after # 移动后下一个位置的元素
|
||
|
end
|
||
|
|
||
|
def call
|
||
|
return if move.position + 1 == after&.position # 未移动
|
||
|
|
||
|
images = PortalImage.all
|
||
|
|
||
|
ActiveRecord::Base.transaction do
|
||
|
if after.blank? # 移动至末尾
|
||
|
total = images.count
|
||
|
|
||
|
images.where('position > ?', move.position).update_all('position = position - 1')
|
||
|
move.update!(position: total)
|
||
|
return
|
||
|
end
|
||
|
|
||
|
if move.position > after.position # 前移
|
||
|
images.where('position >= ? AND position < ?', after.position, move.position).update_all('position = position + 1')
|
||
|
move.update!(position: after.position)
|
||
|
else # 后移
|
||
|
images.where('position > ? AND position <= ?', move.position, after.position).update_all('position = position - 1')
|
||
|
move.update!(position: after.position)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
end
|