Commit f086dbf6 authored by Nick Thomas's avatar Nick Thomas

Add a thin encapsulation around .pluck(:id)

parent 013f7cd2
...@@ -7,6 +7,14 @@ class ApplicationRecord < ActiveRecord::Base ...@@ -7,6 +7,14 @@ class ApplicationRecord < ActiveRecord::Base
where(id: ids) where(id: ids)
end end
def self.id_not_in(ids)
where.not(id: ids)
end
def self.pluck_primary_key
where(nil).pluck(self.primary_key)
end
def self.safe_find_or_create_by!(*args) def self.safe_find_or_create_by!(*args)
safe_find_or_create_by(*args).tap do |record| safe_find_or_create_by(*args).tap do |record|
record.validate! unless record.persisted? record.validate! unless record.persisted?
......
...@@ -76,13 +76,11 @@ class IssuableBaseService < BaseService ...@@ -76,13 +76,11 @@ class IssuableBaseService < BaseService
find_or_create_label_ids find_or_create_label_ids
end end
# rubocop: disable CodeReuse/ActiveRecord
def filter_labels_in_param(key) def filter_labels_in_param(key)
return if params[key].to_a.empty? return if params[key].to_a.empty?
params[key] = available_labels.where(id: params[key]).pluck(:id) params[key] = available_labels.id_in(params[key]).pluck_primary_key
end end
# rubocop: enable CodeReuse/ActiveRecord
def find_or_create_label_ids def find_or_create_label_ids
labels = params.delete(:labels) labels = params.delete(:labels)
......
...@@ -8,15 +8,13 @@ module MergeRequests ...@@ -8,15 +8,13 @@ module MergeRequests
@merge_request = merge_request @merge_request = merge_request
end end
# rubocop: disable CodeReuse/ActiveRecord
def execute def execute
diffs = @merge_request.non_latest_diffs.with_files diffs = @merge_request.non_latest_diffs.with_files
diffs.each_batch(of: BATCH_SIZE) do |relation, index| diffs.each_batch(of: BATCH_SIZE) do |relation, index|
ids = relation.pluck(:id).map { |id| [id] } ids = relation.pluck_primary_key.map { |id| [id] }
DeleteDiffFilesWorker.bulk_perform_in(index * 5.minutes, ids) DeleteDiffFilesWorker.bulk_perform_in(index * 5.minutes, ids)
end end
end end
# rubocop: enable CodeReuse/ActiveRecord
end end
end end
...@@ -26,17 +26,15 @@ module Milestones ...@@ -26,17 +26,15 @@ module Milestones
private private
# rubocop: disable CodeReuse/ActiveRecord
def milestone_ids_for_merge(group_milestone) def milestone_ids_for_merge(group_milestone)
# Pluck need to be used here instead of select so the array of ids # Pluck need to be used here instead of select so the array of ids
# is persistent after old milestones gets deleted. # is persistent after old milestones gets deleted.
@milestone_ids_for_merge ||= begin @milestone_ids_for_merge ||= begin
search_params = { title: group_milestone.title, project_ids: group_project_ids, state: 'all' } search_params = { title: group_milestone.title, project_ids: group_project_ids, state: 'all' }
milestones = MilestonesFinder.new(search_params).execute milestones = MilestonesFinder.new(search_params).execute
milestones.pluck(:id) milestones.pluck_primary_key
end end
end end
# rubocop: enable CodeReuse/ActiveRecord
def move_children_to_group_milestone(group_milestone) def move_children_to_group_milestone(group_milestone)
milestone_ids_for_merge(group_milestone).in_groups_of(100, false) do |milestone_ids| milestone_ids_for_merge(group_milestone).in_groups_of(100, false) do |milestone_ids|
......
...@@ -554,7 +554,7 @@ module QuickActions ...@@ -554,7 +554,7 @@ module QuickActions
current_user.can?(:"update_#{issuable.to_ability_name}", issuable) && current_user.can?(:"update_#{issuable.to_ability_name}", issuable) &&
issuable.project.boards.count == 1 issuable.project.boards.count == 1
end end
# rubocop: disable CodeReuse/ActiveRecord
command :board_move do |target_list_name| command :board_move do |target_list_name|
label_ids = find_label_ids(target_list_name) label_ids = find_label_ids(target_list_name)
...@@ -562,14 +562,17 @@ module QuickActions ...@@ -562,14 +562,17 @@ module QuickActions
label_id = label_ids.first label_id = label_ids.first
# Ensure this label corresponds to a list on the board # Ensure this label corresponds to a list on the board
next unless Label.on_project_boards(issuable.project_id).where(id: label_id).exists? next unless Label.on_project_boards(issuable.project_id).id_in(label_id).exists?
@updates[:remove_label_ids] = issuable
.labels
.on_project_boards(issuable.project_id)
.id_not_in(label_id)
.pluck_primary_key
@updates[:remove_label_ids] =
issuable.labels.on_project_boards(issuable.project_id).where.not(id: label_id).pluck(:id)
@updates[:add_label_ids] = [label_id] @updates[:add_label_ids] = [label_id]
end end
end end
# rubocop: enable CodeReuse/ActiveRecord
desc 'Mark this issue as a duplicate of another issue' desc 'Mark this issue as a duplicate of another issue'
explanation do |duplicate_reference| explanation do |duplicate_reference|
......
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