debian-mirror-gitlab/app/services/issuable/bulk_update_service.rb

47 lines
1.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-09-29 09:46:39 +05:30
module Issuable
class BulkUpdateService < IssuableBaseService
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-09-29 09:46:39 +05:30
def execute(type)
model_class = type.classify.constantize
update_class = type.classify.pluralize.constantize::UpdateService
ids = params.delete(:issuable_ids).split(",")
items = model_class.where(id: ids)
2017-08-17 22:00:37 +05:30
permitted_attrs(type).each do |key|
2016-09-29 09:46:39 +05:30
params.delete(key) unless params[key].present?
end
2017-08-17 22:00:37 +05:30
if params[:assignee_ids] == [IssuableFinder::NONE.to_s]
params[:assignee_ids] = []
end
2016-09-29 09:46:39 +05:30
items.each do |issuable|
next unless can?(current_user, :"update_#{type}", issuable)
update_class.new(issuable.project, current_user, params).execute(issuable)
end
{
count: items.count,
success: !items.count.zero?
}
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
private
def permitted_attrs(type)
attrs = %i(state_event milestone_id assignee_id assignee_ids add_label_ids remove_label_ids subscription_event)
if type == 'issue'
attrs.push(:assignee_ids)
else
attrs.push(:assignee_id)
end
end
2016-09-29 09:46:39 +05:30
end
end