Add service to create a board list

This commit is contained in:
Douglas Barbosa Alexandre 2016-07-27 18:46:03 -03:00
parent a77aee9369
commit 247e11c948
4 changed files with 58 additions and 0 deletions

View file

@ -0,0 +1,22 @@
module Boards
module Lists
class CreateService
def initialize(project, params = {})
@board = project.board
@params = params.dup
end
def execute
board.lists.create(params.merge(position: position))
end
private
attr_reader :board, :params
def position
board.lists.size
end
end
end
end

5
spec/factories/boards.rb Normal file
View file

@ -0,0 +1,5 @@
FactoryGirl.define do
factory :board do
project factory: :empty_project
end
end

7
spec/factories/lists.rb Normal file
View file

@ -0,0 +1,7 @@
FactoryGirl.define do
factory :list do
board
label
sequence(:position)
end
end

View file

@ -0,0 +1,24 @@
require 'spec_helper'
describe Boards::Lists::CreateService, services: true do
describe '#execute' do
let(:project) { create(:project_with_board) }
let(:board) { project.board }
let(:label) { create(:label, name: 'in-progress') }
it 'creates a new list for board' do
service = described_class.new(project, label_id: label.id)
expect { service.execute }.to change(board.lists, :count).by(1)
end
it 'inserts the list to the end of lists' do
create_list(:list, 2, board: board)
service = described_class.new(project, label_id: label.id)
list = service.execute
expect(list.position).to eq 2
end
end
end