Extract base service class for boards related services

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