From c6511235e4253d11447b40c2ea42ecb02c99687e Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Tue, 16 Aug 2016 11:31:21 -0300 Subject: [PATCH] Add a destroyable scope and a destroyable? method to List model --- .../projects/boards/lists_controller.rb | 11 ++++------ app/models/list.rb | 10 ++++++++-- app/services/boards/lists/destroy_service.rb | 2 +- spec/models/list_spec.rb | 20 +++++++++++++++++++ 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/app/controllers/projects/boards/lists_controller.rb b/app/controllers/projects/boards/lists_controller.rb index 7834e12f62d..6555ae6ae37 100644 --- a/app/controllers/projects/boards/lists_controller.rb +++ b/app/controllers/projects/boards/lists_controller.rb @@ -3,7 +3,6 @@ module Projects class ListsController < Boards::ApplicationController before_action :authorize_admin_list!, only: [:create, :update, :destroy, :generate] before_action :authorize_read_list!, only: [:index] - before_action :load_list, only: [:update, :destroy] def index render json: serialize_as_json(project.board.lists) @@ -20,9 +19,10 @@ module Projects end def update + list = project.board.lists.find(params[:id]) service = ::Boards::Lists::MoveService.new(project, current_user, move_params) - if service.execute(@list) + if service.execute(list) head :ok else head :unprocessable_entity @@ -30,9 +30,10 @@ module Projects end def destroy + list = project.board.lists.destroyable.find(params[:id]) service = ::Boards::Lists::DestroyService.new(project, current_user, params) - if service.execute(@list) + if service.execute(list) head :ok else head :unprocessable_entity @@ -59,10 +60,6 @@ module Projects return render_403 unless can?(current_user, :read_list, project) end - def load_list - @list = project.board.lists.find(params[:id]) - end - def list_params params.require(:list).permit(:label_id) end diff --git a/app/models/list.rb b/app/models/list.rb index 41f79513a10..634c012e543 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -9,7 +9,13 @@ class List < ActiveRecord::Base validates :label_id, uniqueness: { scope: :board_id }, if: :label? validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :label? - before_destroy :can_be_destroyed, unless: :label? + before_destroy :can_be_destroyed + + scope :destroyable, -> { where(list_type: list_types[:label]) } + + def destroyable? + label? + end def title label? ? label.name : list_type.humanize @@ -18,6 +24,6 @@ class List < ActiveRecord::Base private def can_be_destroyed - false + destroyable? end end diff --git a/app/services/boards/lists/destroy_service.rb b/app/services/boards/lists/destroy_service.rb index 9edb596bf46..5c8719f193a 100644 --- a/app/services/boards/lists/destroy_service.rb +++ b/app/services/boards/lists/destroy_service.rb @@ -2,7 +2,7 @@ module Boards module Lists class DestroyService < Boards::BaseService def execute(list) - return false unless list.label? + return false unless list.destroyable? list.with_lock do decrement_higher_lists(list) diff --git a/spec/models/list_spec.rb b/spec/models/list_spec.rb index 664d12a2fd3..d5c60f65043 100644 --- a/spec/models/list_spec.rb +++ b/spec/models/list_spec.rb @@ -54,6 +54,26 @@ describe List do end end + describe '#destroyable?' do + it 'retruns true when list_type is set to label' do + subject.list_type = :label + + expect(subject).to be_destroyable + end + + it 'retruns false when list_type is set to backlog' do + subject.list_type = :backlog + + expect(subject).not_to be_destroyable + end + + it 'retruns false when list_type is set to done' do + subject.list_type = :done + + expect(subject).not_to be_destroyable + end + end + describe '#title' do it 'returns label name when list_type is set to label' do subject.list_type = :label -- GitLab