Add a destroyable scope and a destroyable? method to List model

parent 29a91c5b
......@@ -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
......
......@@ -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
......@@ -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)
......
......@@ -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
......
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