WIP: allow adding and removing labels in bulk

This commit is contained in:
Sean McGivern 2016-04-29 17:38:07 +01:00 committed by Alfredo Sumaran
parent e42f88ca37
commit 81a21c945e
3 changed files with 37 additions and 8 deletions

View file

@ -217,7 +217,9 @@ class Projects::IssuesController < Projects::ApplicationController
:assignee_id,
:milestone_id,
:state_event,
label_ids: []
label_ids: [],
add_label_ids: [],
remove_label_ids: []
)
end
end

View file

@ -45,6 +45,8 @@ class IssuableBaseService < BaseService
unless can?(current_user, ability, project)
params.delete(:milestone_id)
params.delete(:add_label_ids)
params.delete(:remove_label_ids)
params.delete(:label_ids)
params.delete(:assignee_id)
end
@ -67,10 +69,35 @@ class IssuableBaseService < BaseService
end
def filter_labels
return if params[:label_ids].to_a.empty?
if params[:add_label_ids].present? || params[:remove_label_ids].present?
params.delete(:label_ids)
params[:label_ids] =
project.labels.where(id: params[:label_ids]).pluck(:id)
filter_labels_by_name([:add_label_ids, :remove_label_ids])
else
filter_labels_by_name([:label_ids])
end
end
def filter_labels_by_name(keys)
keys.each do |key|
return if params[key].to_a.empty?
params[key] = project.labels.where(id: params[key]).pluck(:id)
end
end
def update_issuable(issuable, attributes)
issuable.with_transaction_returning_status do
add_label_ids = attributes.delete(:add_label_ids)
remove_label_ids = attributes.delete(:remove_label_ids)
issuable.label_ids |= add_label_ids if add_label_ids
issuable.label_ids -= remove_label_ids if remove_label_ids
issuable.assign_attributes(attributes)
issuable.save
end
end
def update(issuable)
@ -78,7 +105,7 @@ class IssuableBaseService < BaseService
filter_params
old_labels = issuable.labels.to_a
if params.present? && issuable.update_attributes(params.merge(updated_by: current_user))
if params.present? && update_issuable(issuable, params.merge(updated_by: current_user))
issuable.reset_events_cache
handle_common_system_notes(issuable, old_labels: old_labels)
handle_changes(issuable, old_labels: old_labels)

View file

@ -4,9 +4,9 @@ module Issues
issues_ids = params.delete(:issues_ids).split(",")
issue_params = params
issue_params.delete(:state_event) unless issue_params[:state_event].present?
issue_params.delete(:milestone_id) unless issue_params[:milestone_id].present?
issue_params.delete(:assignee_id) unless issue_params[:assignee_id].present?
[:state_event, :milestone_id, :assignee_id, :label_ids, :add_label_ids, :remove_label_ids].each do |key|
issue_params.delete(key) unless issue_params[key].present?
end
issues = Issue.where(id: issues_ids)
issues.each do |issue|