Update Boards::CreateService to allow projects to have many boards

parent 1df2be43
module Boards
class CreateService < BaseService
def execute
if project.boards.empty?
create_board!
else
project.boards.first
end
end
board = project.boards.create(params)
private
def create_board!
board = project.boards.create
board.lists.create(list_type: :backlog)
board.lists.create(list_type: :done)
if board.persisted?
board.lists.create(list_type: :backlog)
board.lists.create(list_type: :done)
end
board
end
......
......@@ -4,14 +4,14 @@ describe Boards::CreateService, services: true do
describe '#execute' do
let(:project) { create(:empty_project) }
subject(:service) { described_class.new(project, double) }
context 'with valid params' do
subject(:service) { described_class.new(project, double, name: 'Backend') }
context 'when project does not have a board' do
it 'creates a new board' do
expect { service.execute }.to change(Board, :count).by(1)
it 'creates a new project board' do
expect { service.execute }.to change(project.boards, :count).by(1)
end
it 'creates default lists' do
it "creates board's default lists" do
board = service.execute
expect(board.lists.size).to eq 2
......@@ -20,14 +20,34 @@ describe Boards::CreateService, services: true do
end
end
context 'when project has a board' do
before do
create(:board, project: project)
end
context 'with invalid params' do
subject(:service) { described_class.new(project, double, name: nil) }
it 'does not create a new board' do
it 'does not create a new project board' do
expect { service.execute }.not_to change(project.boards, :count)
end
it "does not create board's default lists" do
board = service.execute
expect(board.lists.size).to eq 0
end
end
context 'without params' do
subject(:service) { described_class.new(project, double) }
it 'creates a new project board' do
expect { service.execute }.to change(project.boards, :count).by(1)
end
it "creates board's default lists" do
board = service.execute
expect(board.lists.size).to eq 2
expect(board.lists.first).to be_backlog
expect(board.lists.last).to be_done
end
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