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

parent c6511235
...@@ -19,7 +19,7 @@ module Projects ...@@ -19,7 +19,7 @@ module Projects
end end
def update def update
list = project.board.lists.find(params[:id]) list = project.board.lists.movable.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)
...@@ -44,7 +44,7 @@ module Projects ...@@ -44,7 +44,7 @@ module Projects
service = ::Boards::Lists::GenerateService.new(project, current_user) service = ::Boards::Lists::GenerateService.new(project, current_user)
if service.execute if service.execute
render json: serialize_as_json(project.board.lists.label) render json: serialize_as_json(project.board.lists.movable)
else else
head :unprocessable_entity head :unprocessable_entity
end end
......
...@@ -12,11 +12,16 @@ class List < ActiveRecord::Base ...@@ -12,11 +12,16 @@ class List < ActiveRecord::Base
before_destroy :can_be_destroyed before_destroy :can_be_destroyed
scope :destroyable, -> { where(list_type: list_types[:label]) } scope :destroyable, -> { where(list_type: list_types[:label]) }
scope :movable, -> { where(list_type: list_types[:label]) }
def destroyable? def destroyable?
label? label?
end end
def movable?
label?
end
def title def title
label? ? label.name : list_type.humanize label? ? label.name : list_type.humanize
end end
......
...@@ -3,8 +3,8 @@ module Boards ...@@ -3,8 +3,8 @@ module Boards
class ListService < Boards::BaseService class ListService < Boards::BaseService
def execute def execute
issues = IssuesFinder.new(user, filter_params).execute issues = IssuesFinder.new(user, filter_params).execute
issues = without_board_labels(issues) unless list.label? issues = without_board_labels(issues) unless list.movable?
issues = with_list_label(issues) if list.label? issues = with_list_label(issues) if list.movable?
issues issues
end end
...@@ -40,7 +40,7 @@ module Boards ...@@ -40,7 +40,7 @@ module Boards
end end
def board_label_ids def board_label_ids
@board_label_ids ||= board.lists.label.pluck(:label_id) @board_label_ids ||= board.lists.movable.pluck(:label_id)
end end
def without_board_labels(issues) def without_board_labels(issues)
......
...@@ -45,10 +45,10 @@ module Boards ...@@ -45,10 +45,10 @@ module Boards
def remove_label_ids def remove_label_ids
label_ids = label_ids =
if moving_to_list.label? if moving_to_list.movable?
moving_from_list.label_id moving_from_list.label_id
else else
board.lists.label.pluck(:label_id) board.lists.movable.pluck(:label_id)
end end
Array(label_ids).compact Array(label_ids).compact
......
...@@ -10,7 +10,7 @@ module Boards ...@@ -10,7 +10,7 @@ module Boards
private private
def next_position def next_position
max_position = board.lists.label.maximum(:position) max_position = board.lists.movable.maximum(:position)
max_position.nil? ? 0 : max_position.succ max_position.nil? ? 0 : max_position.succ
end end
......
...@@ -13,7 +13,7 @@ module Boards ...@@ -13,7 +13,7 @@ module Boards
private private
def decrement_higher_lists(list) def decrement_higher_lists(list)
board.lists.label.where('position > ?', list.position) board.lists.movable.where('position > ?', list.position)
.update_all('position = position - 1') .update_all('position = position - 1')
end end
......
...@@ -2,7 +2,7 @@ module Boards ...@@ -2,7 +2,7 @@ module Boards
module Lists module Lists
class GenerateService < Boards::BaseService class GenerateService < Boards::BaseService
def execute def execute
return false unless board.lists.label.empty? return false unless board.lists.movable.empty?
List.transaction do List.transaction do
label_params.each { |params| create_list(params) } label_params.each { |params| create_list(params) }
......
...@@ -5,7 +5,7 @@ module Boards ...@@ -5,7 +5,7 @@ module Boards
@old_position = list.position @old_position = list.position
@new_position = params[:position] @new_position = params[:position]
return false unless list.label? return false unless list.movable?
return false unless valid_move? return false unless valid_move?
list.with_lock do list.with_lock do
...@@ -20,7 +20,7 @@ module Boards ...@@ -20,7 +20,7 @@ module Boards
def valid_move? def valid_move?
new_position.present? && new_position != old_position && new_position.present? && new_position != old_position &&
new_position >= 0 && new_position < board.lists.label.size new_position >= 0 && new_position < board.lists.movable.size
end end
def reorder_intermediate_lists def reorder_intermediate_lists
...@@ -32,15 +32,15 @@ module Boards ...@@ -32,15 +32,15 @@ module Boards
end end
def decrement_intermediate_lists def decrement_intermediate_lists
board.lists.label.where('position > ?', old_position) board.lists.movable.where('position > ?', old_position)
.where('position <= ?', new_position) .where('position <= ?', new_position)
.update_all('position = position - 1') .update_all('position = position - 1')
end end
def increment_intermediate_lists def increment_intermediate_lists
board.lists.label.where('position >= ?', new_position) board.lists.movable.where('position >= ?', new_position)
.where('position < ?', old_position) .where('position < ?', old_position)
.update_all('position = position + 1') .update_all('position = position + 1')
end end
def update_list_position(list) def update_list_position(list)
......
...@@ -74,6 +74,26 @@ describe List do ...@@ -74,6 +74,26 @@ describe List do
end end
end end
describe '#movable?' do
it 'retruns true when list_type is set to label' do
subject.list_type = :label
expect(subject).to be_movable
end
it 'retruns false when list_type is set to backlog' do
subject.list_type = :backlog
expect(subject).not_to be_movable
end
it 'retruns false when list_type is set to done' do
subject.list_type = :done
expect(subject).not_to be_movable
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