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

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