Extract base service class for boards related services

parent 296bcbd6
......@@ -4,7 +4,7 @@ class Projects::BoardListsController < Projects::ApplicationController
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
def create
list = Boards::Lists::CreateService.new(project, list_params).execute
list = Boards::Lists::CreateService.new(project, current_user, list_params).execute
if list.valid?
render json: list.as_json(only: [:id, :list_type, :position], methods: [:title], include: { label: { only: [:id, :title, :color] } })
......@@ -14,7 +14,7 @@ class Projects::BoardListsController < Projects::ApplicationController
end
def update
service = Boards::Lists::MoveService.new(project, move_params)
service = Boards::Lists::MoveService.new(project, current_user, move_params)
if service.execute
head :ok
......@@ -24,7 +24,7 @@ class Projects::BoardListsController < Projects::ApplicationController
end
def destroy
service = Boards::Lists::DestroyService.new(project, params)
service = Boards::Lists::DestroyService.new(project, current_user, params)
if service.execute
head :ok
......
class Projects::BoardsController < Projects::ApplicationController
def show
board = Boards::CreateService.new(project).execute
board = Boards::CreateService.new(project, current_user).execute
respond_to do |format|
format.html
......
module Boards
class BaseService
def initialize(project, user, params = {})
@project = project
@board = project.board
@user = user
@params = params.dup
end
private
attr_reader :project, :board, :user, :params
end
end
module Boards
class CreateService
def initialize(project)
@project = project
end
class CreateService < BaseService
def execute
create_board! unless project.board.present?
project.board
......@@ -11,8 +7,6 @@ module Boards
private
attr_reader :project
def create_board!
project.create_board
project.board.lists.create(list_type: :backlog)
......
module Boards
module Issues
class ListService
def initialize(project, user, params = {})
@project = project
@user = user
@params = params.dup
end
class ListService < Boards::BaseService
def execute
issues = IssuesFinder.new(user, filter_params).execute
issues = without_board_labels(issues) if list.backlog?
......@@ -15,10 +9,6 @@ module Boards
private
attr_reader :project, :user, :params
delegate :board, to: :project
def list
@list ||= board.lists.find(params[:list_id])
end
......
module Boards
module Issues
class MoveService
def initialize(project, user, params = {})
@project = project
@user = user
@params = params.dup
end
class MoveService < Boards::BaseService
def execute
return false unless issue.present?
return false unless user.can?(:update_issue, issue)
......@@ -20,10 +14,6 @@ module Boards
private
attr_reader :project, :user, :params
delegate :board, to: :project
def issue
@issue ||= project.issues.visible_to_user(user).find_by!(iid: params[:id])
end
......
module Boards
module Lists
class CreateService
def initialize(project, params = {})
@board = project.board
@params = params.dup
end
class CreateService < Boards::BaseService
def execute
List.transaction do
position = find_next_position
......@@ -16,8 +11,6 @@ module Boards
private
attr_reader :board, :params
def find_next_position
board.lists.label.maximum(:position).to_i + 1
end
......
module Boards
module Lists
class DestroyService
def initialize(project, params = {})
@board = project.board
@params = params.dup
end
class DestroyService < Boards::BaseService
def execute
return false unless list.label?
......@@ -17,8 +12,6 @@ module Boards
private
attr_reader :board, :params
def list
@list ||= board.lists.find(params[:id])
end
......
module Boards
module Lists
class MoveService
def initialize(project, params = {})
@board = project.board
@params = params.dup
end
class MoveService < Boards::BaseService
def execute
return false unless list.label?
return false unless valid_move?
......@@ -18,8 +13,6 @@ module Boards
private
attr_reader :board, :params
def list
@list ||= board.lists.find(params[:id])
end
......
......@@ -2,7 +2,7 @@ require 'spec_helper'
describe Boards::CreateService, services: true do
describe '#execute' do
subject(:service) { described_class.new(project) }
subject(:service) { described_class.new(project, double) }
context 'when project does not have a board' do
let(:project) { create(:empty_project, board: nil) }
......
......@@ -4,9 +4,10 @@ describe Boards::Lists::CreateService, services: true do
describe '#execute' do
let(:project) { create(:project_with_board) }
let(:board) { project.board }
let(:user) { create(:user) }
let(:label) { create(:label, name: 'in-progress') }
subject(:service) { described_class.new(project, label_id: label.id) }
subject(:service) { described_class.new(project, user, label_id: label.id) }
context 'when board lists is empty' do
it 'creates a new list at beginning of the list' do
......@@ -31,7 +32,7 @@ describe Boards::Lists::CreateService, services: true do
create(:list, board: board, position: 1)
create(:list, board: board, position: 2)
list = described_class.new(project, label_id: label.id).execute
list = service.execute
expect(list.position).to eq 3
end
......@@ -43,7 +44,7 @@ describe Boards::Lists::CreateService, services: true do
create(:done_list, board: board)
list1 = create(:list, board: board, position: 1)
list2 = described_class.new(project, label_id: label.id).execute
list2 = service.execute
expect(list1.reload.position).to eq 1
expect(list2.reload.position).to eq 2
......
......@@ -4,11 +4,12 @@ describe Boards::Lists::DestroyService, services: true do
describe '#execute' do
let(:project) { create(:project_with_board) }
let(:board) { project.board }
let(:user) { create(:user) }
context 'when list type is label' do
it 'removes list from board' do
list = create(:list, board: board)
service = described_class.new(project, id: list.id)
service = described_class.new(project, user, id: list.id)
expect { service.execute }.to change(board.lists, :count).by(-1)
end
......@@ -20,7 +21,7 @@ describe Boards::Lists::DestroyService, services: true do
staging = create(:list, board: board, position: 3)
done = create(:done_list, board: board)
described_class.new(project, id: development.id).execute
described_class.new(project, user, id: development.id).execute
expect(backlog.reload.position).to be_nil
expect(review.reload.position).to eq 1
......@@ -31,14 +32,14 @@ describe Boards::Lists::DestroyService, services: true do
it 'does not remove list from board when list type is backlog' do
list = create(:backlog_list, board: board)
service = described_class.new(project, id: list.id)
service = described_class.new(project, user, id: list.id)
expect { service.execute }.not_to change(board.lists, :count)
end
it 'does not remove list from board when list type is done' do
list = create(:done_list, board: board)
service = described_class.new(project, id: list.id)
service = described_class.new(project, user, id: list.id)
expect { service.execute }.not_to change(board.lists, :count)
end
......
......@@ -4,6 +4,7 @@ describe Boards::Lists::MoveService, services: true do
describe '#execute' do
let(:project) { create(:project_with_board) }
let(:board) { project.board }
let(:user) { create(:user) }
let!(:backlog) { create(:backlog_list, board: board) }
let!(:planning) { create(:list, board: board, position: 1) }
......@@ -14,7 +15,7 @@ describe Boards::Lists::MoveService, services: true do
context 'when list type is set to label' do
it 'keeps position of lists when new position is nil' do
service = described_class.new(project, { id: planning.id, position: nil })
service = described_class.new(project, user, id: planning.id, position: nil)
service.execute
......@@ -22,7 +23,7 @@ describe Boards::Lists::MoveService, services: true do
end
it 'keeps position of lists when new positon is equal to old position' do
service = described_class.new(project, { id: planning.id, position: 1 })
service = described_class.new(project, user, id: planning.id, position: 1)
service.execute
......@@ -30,7 +31,7 @@ describe Boards::Lists::MoveService, services: true do
end
it 'keeps position of lists when new positon is negative' do
service = described_class.new(project, { id: planning.id, position: -1 })
service = described_class.new(project, user, id: planning.id, position: -1)
service.execute
......@@ -38,7 +39,7 @@ describe Boards::Lists::MoveService, services: true do
end
it 'keeps position of lists when new positon is greater than number of labels lists' do
service = described_class.new(project, { id: planning.id, position: 6 })
service = described_class.new(project, user, id: planning.id, position: 6)
service.execute
......@@ -46,7 +47,7 @@ describe Boards::Lists::MoveService, services: true do
end
it 'increments position of intermediate lists when new positon is equal to first position' do
service = described_class.new(project, { id: staging.id, position: 1 })
service = described_class.new(project, user, id: staging.id, position: 1)
service.execute
......@@ -54,7 +55,7 @@ describe Boards::Lists::MoveService, services: true do
end
it 'decrements position of intermediate lists when new positon is equal to last position' do
service = described_class.new(project, { id: planning.id, position: 4 })
service = described_class.new(project, user, id: planning.id, position: 4)
service.execute
......@@ -62,7 +63,7 @@ describe Boards::Lists::MoveService, services: true do
end
it 'decrements position of intermediate lists when new position is greater than old position' do
service = described_class.new(project, { id: planning.id, position: 3 })
service = described_class.new(project, user, id: planning.id, position: 3)
service.execute
......@@ -70,7 +71,7 @@ describe Boards::Lists::MoveService, services: true do
end
it 'increments position of intermediate lists when new position is lower than old position' do
service = described_class.new(project, { id: staging.id, position: 2 })
service = described_class.new(project, user, id: staging.id, position: 2)
service.execute
......@@ -79,7 +80,7 @@ describe Boards::Lists::MoveService, services: true do
end
it 'keeps position of lists when list type is backlog' do
service = described_class.new(project, { id: backlog.id, position: 2 })
service = described_class.new(project, user, id: backlog.id, position: 2)
service.execute
......@@ -87,7 +88,7 @@ describe Boards::Lists::MoveService, services: true do
end
it 'keeps position of lists when list type is done' do
service = described_class.new(project, { id: done.id, position: 2 })
service = described_class.new(project, user, id: done.id, position: 2)
service.execute
......
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