Add service to move board lists

parent 247e11c9
module Boards
module Lists
class MoveService
def initialize(project, params = {})
@board = project.board
@params = params.dup
end
def execute
return false if new_position.blank?
return false if new_position == old_position
list.with_lock do
reorder_intermediate_lists
update_list_position
end
end
private
attr_reader :board, :params
def list
@list ||= board.lists.find(params[:list_id])
end
def old_position
@old_position ||= list.position
end
def new_position
@new_position ||= params[:position]
end
def reorder_intermediate_lists
if old_position < new_position
board.lists.where('position > ?', old_position)
.where('position <= ?', new_position)
.update_all('position = position - 1')
else
board.lists.where('position >= ?', new_position)
.where('position < ?', old_position)
.update_all('position = position + 1')
end
end
def update_list_position
list.update_attribute(:position, new_position)
end
end
end
end
require 'spec_helper'
describe Boards::Lists::MoveService, services: true do
describe '#execute' do
let(:project) { create(:project_with_board) }
let(:board) { project.board }
it 'keeps position of lists when new position is nil' do
list1 = create(:list, board: board, position: 1)
list2 = create(:list, board: board, position: 2)
list3 = create(:list, board: board, position: 3)
list4 = create(:list, board: board, position: 4)
list5 = create(:list, board: board, position: 5)
service = described_class.new(project, { list_id: list2.id, position: nil })
expect(service.execute).to eq false
expect(list1.reload.position).to eq 1
expect(list2.reload.position).to eq 2
expect(list3.reload.position).to eq 3
expect(list4.reload.position).to eq 4
expect(list5.reload.position).to eq 5
end
it 'keeps position of lists when new positon is equal to old position' do
list1 = create(:list, board: board, position: 1)
list2 = create(:list, board: board, position: 2)
list3 = create(:list, board: board, position: 3)
list4 = create(:list, board: board, position: 4)
list5 = create(:list, board: board, position: 5)
service = described_class.new(project, { list_id: list2.id, position: 2 })
expect(service.execute).to eq false
expect(list1.reload.position).to eq 1
expect(list2.reload.position).to eq 2
expect(list3.reload.position).to eq 3
expect(list4.reload.position).to eq 4
expect(list5.reload.position).to eq 5
end
it 'decrements position of intermediate lists when new position is greater than old position' do
list1 = create(:list, board: board, position: 1)
list2 = create(:list, board: board, position: 2)
list3 = create(:list, board: board, position: 3)
list4 = create(:list, board: board, position: 4)
list5 = create(:list, board: board, position: 5)
service = described_class.new(project, { list_id: list2.id, position: 5 })
expect(service.execute).to eq true
expect(list1.reload.position).to eq 1
expect(list2.reload.position).to eq 5
expect(list3.reload.position).to eq 2
expect(list4.reload.position).to eq 3
expect(list5.reload.position).to eq 4
end
it 'increments position of intermediate lists when when new position is lower than old position' do
list1 = create(:list, board: board, position: 1)
list2 = create(:list, board: board, position: 2)
list3 = create(:list, board: board, position: 3)
list4 = create(:list, board: board, position: 4)
list5 = create(:list, board: board, position: 5)
service = described_class.new(project, { list_id: list5.id, position: 2 })
expect(service.execute).to eq true
expect(list1.reload.position).to eq 1
expect(list2.reload.position).to eq 3
expect(list3.reload.position).to eq 4
expect(list4.reload.position).to eq 5
expect(list5.reload.position).to eq 2
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment