Commit e3ab08be authored by Felipe Artur's avatar Felipe Artur

Allow to update epic board lists

Introduce EpicLists::UpdateService
parent 2895caf8
# frozen_string_literal: true
module Boards
module Lists
class BaseUpdateService < Boards::BaseService
def execute(list)
if execute_by_params(list)
success(list: list)
else
error(list.errors.messages, 422)
end
end
private
def execute_by_params(list)
update_preferences_result = update_preferences(list) if can_read?(list)
update_position_result = update_position(list) if can_admin?(list)
update_preferences_result || update_position_result
end
def update_preferences(list)
return unless preferences?
list.update_preferences_for(current_user, preferences)
end
def update_position(list)
return unless position?
move_service = Boards::Lists::MoveService.new(parent, current_user, params)
move_service.execute(list)
end
def preferences
{ collapsed: Gitlab::Utils.to_boolean(params[:collapsed]) }
end
def preferences?
params.has_key?(:collapsed)
end
def position?
params.has_key?(:position)
end
def can_read?(list)
raise NotImplementedError
end
def can_admin?(list)
raise NotImplementedError
end
end
end
end
......@@ -2,50 +2,7 @@
module Boards
module Lists
class UpdateService < Boards::BaseService
def execute(list)
if execute_by_params(list)
success(list: list)
else
error(list.errors.messages, 422)
end
end
private
def execute_by_params(list)
update_preferences_result = update_preferences(list) if can_read?(list)
update_position_result = update_position(list) if can_admin?(list)
update_preferences_result || update_position_result
end
def update_preferences(list)
return unless preferences?
list.update_preferences_for(current_user, preferences)
end
def update_position(list)
return unless position?
move_service = Boards::Lists::MoveService.new(parent, current_user, params)
move_service.execute(list)
end
def preferences
{ collapsed: Gitlab::Utils.to_boolean(params[:collapsed]) }
end
def preferences?
params.has_key?(:collapsed)
end
def position?
params.has_key?(:position)
end
class UpdateService < Boards::Lists::BaseUpdateService
def can_read?(list)
Ability.allowed?(current_user, :read_issue_board_list, parent)
end
......
......@@ -29,5 +29,9 @@ module Boards
end
end
end
def board
epic_board
end
end
end
# frozen_string_literal: true
module Boards
module EpicLists
class UpdateService < ::Boards::Lists::BaseUpdateService
def can_read?(list)
Ability.allowed?(current_user, :read_epic_board_list, parent)
end
def can_admin?(list)
Ability.allowed?(current_user, :admin_epic_board_list, parent)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Boards::EpicLists::UpdateService do
let_it_be(:user) { create(:user) }
let_it_be(:group) { create(:group, :private) }
let_it_be(:board) { create(:epic_board, group: group) }
let_it_be(:list) { create(:epic_list, epic_board: board, position: 0) }
before do
stub_licensed_features(epics: true)
end
describe '#execute' do
let(:service) { described_class.new(board.resource_parent, user, params) }
context 'when position parameter is present' do
let(:params) { { position: 1 } }
it_behaves_like 'moving list'
end
context 'when collapsed parameter is present' do
let(:params) { { collapsed: true } }
it_behaves_like 'updating list preferences'
end
context 'when position and collapsed are both present' do
let(:params) { { collapsed: true, position: 1 } }
it_behaves_like 'moving list'
it_behaves_like 'updating list preferences'
end
end
end
......@@ -6,47 +6,6 @@ RSpec.describe Boards::Lists::UpdateService do
let(:user) { create(:user) }
let!(:list) { create(:list, board: board, position: 0) }
shared_examples 'moving list' do
context 'when user can admin list' do
it 'calls Lists::MoveService to update list position' do
board.resource_parent.add_developer(user)
expect(Boards::Lists::MoveService).to receive(:new).with(board.resource_parent, user, params).and_call_original
expect_any_instance_of(Boards::Lists::MoveService).to receive(:execute).with(list)
service.execute(list)
end
end
context 'when user cannot admin list' do
it 'does not call Lists::MoveService to update list position' do
expect(Boards::Lists::MoveService).not_to receive(:new)
service.execute(list)
end
end
end
shared_examples 'updating list preferences' do
context 'when user can read list' do
it 'updates list preference for user' do
board.resource_parent.add_guest(user)
service.execute(list)
expect(list.preferences_for(user).collapsed).to eq(true)
end
end
context 'when user cannot read list' do
it 'does not update list preference for user' do
service.execute(list)
expect(list.preferences_for(user).collapsed).to be_nil
end
end
end
describe '#execute' do
let(:service) { described_class.new(board.resource_parent, user, params) }
......
# frozen_string_literal: true
RSpec.shared_examples 'moving list' do
context 'when user can admin list' do
it 'calls Lists::MoveService to update list position' do
board.resource_parent.add_developer(user)
expect_next_instance_of(Boards::Lists::MoveService, board.resource_parent, user, params) do |move_service|
expect(move_service).to receive(:execute).with(list).and_call_original
end
service.execute(list)
end
end
context 'when user cannot admin list' do
it 'does not call Lists::MoveService to update list position' do
expect(Boards::Lists::MoveService).not_to receive(:new)
service.execute(list)
end
end
end
RSpec.shared_examples 'updating list preferences' do
context 'when user can read list' do
it 'updates list preference for user' do
board.resource_parent.add_guest(user)
service.execute(list)
expect(list.preferences_for(user).collapsed).to eq(true)
end
end
context 'when user cannot read list' do
it 'does not update list preference for user' do
service.execute(list)
expect(list.preferences_for(user).collapsed).to be_falsy
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