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

parent 29a91c5b
...@@ -3,7 +3,6 @@ module Projects ...@@ -3,7 +3,6 @@ module Projects
class ListsController < Boards::ApplicationController class ListsController < Boards::ApplicationController
before_action :authorize_admin_list!, only: [:create, :update, :destroy, :generate] before_action :authorize_admin_list!, only: [:create, :update, :destroy, :generate]
before_action :authorize_read_list!, only: [:index] before_action :authorize_read_list!, only: [:index]
before_action :load_list, only: [:update, :destroy]
def index def index
render json: serialize_as_json(project.board.lists) render json: serialize_as_json(project.board.lists)
...@@ -20,9 +19,10 @@ module Projects ...@@ -20,9 +19,10 @@ module Projects
end end
def update def update
list = project.board.lists.find(params[:id])
service = ::Boards::Lists::MoveService.new(project, current_user, move_params) service = ::Boards::Lists::MoveService.new(project, current_user, move_params)
if service.execute(@list) if service.execute(list)
head :ok head :ok
else else
head :unprocessable_entity head :unprocessable_entity
...@@ -30,9 +30,10 @@ module Projects ...@@ -30,9 +30,10 @@ module Projects
end end
def destroy def destroy
list = project.board.lists.destroyable.find(params[:id])
service = ::Boards::Lists::DestroyService.new(project, current_user, params) service = ::Boards::Lists::DestroyService.new(project, current_user, params)
if service.execute(@list) if service.execute(list)
head :ok head :ok
else else
head :unprocessable_entity head :unprocessable_entity
...@@ -59,10 +60,6 @@ module Projects ...@@ -59,10 +60,6 @@ module Projects
return render_403 unless can?(current_user, :read_list, project) return render_403 unless can?(current_user, :read_list, project)
end end
def load_list
@list = project.board.lists.find(params[:id])
end
def list_params def list_params
params.require(:list).permit(:label_id) params.require(:list).permit(:label_id)
end end
......
...@@ -9,7 +9,13 @@ class List < ActiveRecord::Base ...@@ -9,7 +9,13 @@ class List < ActiveRecord::Base
validates :label_id, uniqueness: { scope: :board_id }, if: :label? validates :label_id, uniqueness: { scope: :board_id }, if: :label?
validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, 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 def title
label? ? label.name : list_type.humanize label? ? label.name : list_type.humanize
...@@ -18,6 +24,6 @@ class List < ActiveRecord::Base ...@@ -18,6 +24,6 @@ class List < ActiveRecord::Base
private private
def can_be_destroyed def can_be_destroyed
false destroyable?
end end
end end
...@@ -2,7 +2,7 @@ module Boards ...@@ -2,7 +2,7 @@ module Boards
module Lists module Lists
class DestroyService < Boards::BaseService class DestroyService < Boards::BaseService
def execute(list) def execute(list)
return false unless list.label? return false unless list.destroyable?
list.with_lock do list.with_lock do
decrement_higher_lists(list) decrement_higher_lists(list)
......
...@@ -54,6 +54,26 @@ describe List do ...@@ -54,6 +54,26 @@ describe List do
end end
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 describe '#title' do
it 'returns label name when list_type is set to label' do it 'returns label name when list_type is set to label' do
subject.list_type = :label 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