Commit e9cfca05 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Refactor assignee filtering

Extract issuable assignee filters to separate class
parent 3a95b615
...@@ -148,7 +148,6 @@ class IssuableFinder ...@@ -148,7 +148,6 @@ class IssuableFinder
# Negates all params found in `negatable_params` # Negates all params found in `negatable_params`
def filter_negated_items(items) def filter_negated_items(items)
items = by_negated_assignee(items)
items = by_negated_label(items) items = by_negated_label(items)
items = by_negated_milestone(items) items = by_negated_milestone(items)
items = by_negated_release(items) items = by_negated_release(items)
...@@ -365,32 +364,21 @@ class IssuableFinder ...@@ -365,32 +364,21 @@ class IssuableFinder
def by_author(items) def by_author(items)
Issuables::AuthorFilter.new( Issuables::AuthorFilter.new(
items,
params: original_params, params: original_params,
or_filters_enabled: or_filters_enabled? or_filters_enabled: or_filters_enabled?
).filter ).filter(items)
end end
def by_assignee(items) def by_assignee(items)
if params.filter_by_no_assignee? assignee_filter.filter(items)
items.unassigned
elsif params.filter_by_any_assignee?
items.assigned
elsif params.assignee
items.assigned_to(params.assignee)
elsif params.assignee_id? || params.assignee_username? # assignee not found
items.none
else
items
end
end end
def by_negated_assignee(items) def assignee_filter
# We want CE users to be able to say "Issues not assigned to either PersonA nor PersonB" strong_memoize(:assignee_filter) do
if not_params.assignees.present? Issuables::AssigneeFilter.new(
items.not_assigned_to(not_params.assignees) params: original_params,
else or_filters_enabled: or_filters_enabled?
items )
end end
end end
......
...@@ -27,14 +27,6 @@ class IssuableFinder ...@@ -27,14 +27,6 @@ class IssuableFinder
params.present? params.present?
end end
def filter_by_no_assignee?
params[:assignee_id].to_s.downcase == FILTER_NONE
end
def filter_by_any_assignee?
params[:assignee_id].to_s.downcase == FILTER_ANY
end
def filter_by_no_label? def filter_by_no_label?
downcased = label_names.map(&:downcase) downcased = label_names.map(&:downcase)
...@@ -156,24 +148,6 @@ class IssuableFinder ...@@ -156,24 +148,6 @@ class IssuableFinder
end end
end end
# rubocop: disable CodeReuse/ActiveRecord
def assignees
strong_memoize(:assignees) do
if assignee_id?
User.where(id: params[:assignee_id])
elsif assignee_username?
User.where(username: params[:assignee_username])
else
User.none
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
def assignee
assignees.first
end
def label_names def label_names
if labels? if labels?
params[:label_name].is_a?(String) ? params[:label_name].split(',') : params[:label_name] params[:label_name].is_a?(String) ? params[:label_name].split(',') : params[:label_name]
......
# frozen_string_literal: true
module Issuables
class AssigneeFilter < BaseFilter
def filter(issuables)
filtered = by_assignee(issuables)
by_negated_assignee(filtered)
end
def includes_user?(user)
Array(params[:assignee_ids]).include?(user.id) ||
Array(params[:assignee_id]).include?(user.id) ||
Array(params[:assignee_username]).include?(user.username)
end
private
def by_assignee(issuables)
if filter_by_no_assignee?
issuables.unassigned
elsif filter_by_any_assignee?
issuables.assigned
elsif has_assignee_param?(params)
filter_by_assignees(issuables)
else
issuables
end
end
def by_negated_assignee(issuables)
return issuables unless has_assignee_param?(not_params)
issuables.not_assigned_to(assignee_ids(not_params))
end
def filter_by_no_assignee?
params[:assignee_id].to_s.downcase == FILTER_NONE
end
def filter_by_any_assignee?
params[:assignee_id].to_s.downcase == FILTER_ANY
end
def filter_by_assignees(issuables)
assignee_ids = assignee_ids(params)
return issuables.none if assignee_ids.blank?
assignee_ids.each do |assignee_id|
issuables = issuables.assigned_to(assignee_id)
end
issuables
end
def has_assignee_param?(specific_params)
return if specific_params.nil?
specific_params[:assignee_ids].present? || specific_params[:assignee_id].present? || specific_params[:assignee_username].present?
end
def assignee_ids(specific_params)
if specific_params[:assignee_ids].present?
Array(specific_params[:assignee_ids])
elsif specific_params[:assignee_id].present?
Array(specific_params[:assignee_id])
elsif specific_params[:assignee_username].present?
User.by_username(specific_params[:assignee_username]).select(:id)
end
end
end
end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module Issuables module Issuables
class AuthorFilter < BaseFilter class AuthorFilter < BaseFilter
def filter def filter(issuables)
filtered = by_author(issuables) filtered = by_author(issuables)
filtered = by_author_union(filtered) filtered = by_author_union(filtered)
by_negated_author(filtered) by_negated_author(filtered)
......
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
module Issuables module Issuables
class BaseFilter class BaseFilter
attr_reader :issuables, :params attr_reader :params
def initialize(issuables, params:, or_filters_enabled: false) FILTER_NONE = 'none'
@issuables = issuables FILTER_ANY = 'any'
def initialize(params:, or_filters_enabled: false)
@params = params @params = params
@or_filters_enabled = or_filters_enabled @or_filters_enabled = or_filters_enabled
end end
......
...@@ -52,7 +52,7 @@ class IssuesFinder < IssuableFinder ...@@ -52,7 +52,7 @@ class IssuesFinder < IssuableFinder
# can always see confidential issues assigned to them. This is just an # can always see confidential issues assigned to them. This is just an
# optimization since a very common usecase of this Finder is to load the # optimization since a very common usecase of this Finder is to load the
# count of issues assigned to the user for the header bar. # count of issues assigned to the user for the header bar.
return Issue.all if current_user && params.assignees.include?(current_user) return Issue.all if current_user && assignee_filter.includes_user?(current_user)
return Issue.where('issues.confidential IS NOT TRUE') if params.user_cannot_see_confidential_issues? return Issue.where('issues.confidential IS NOT TRUE') if params.user_cannot_see_confidential_issues?
......
...@@ -43,19 +43,6 @@ module EE ...@@ -43,19 +43,6 @@ module EE
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
override :by_assignee
def by_assignee(items)
if params.assignees.any?
params.assignees.each do |assignee|
items = items.assigned_to(assignee)
end
return items
end
super
end
def by_epic(items) def by_epic(items)
return items unless params.by_epic? return items unless params.by_epic?
......
...@@ -30,19 +30,6 @@ module EE ...@@ -30,19 +30,6 @@ module EE
params[:weight].to_s.downcase == ::IssuableFinder::Params::FILTER_ANY params[:weight].to_s.downcase == ::IssuableFinder::Params::FILTER_ANY
end end
override :assignees
# rubocop: disable CodeReuse/ActiveRecord
def assignees
strong_memoize(:assignees) do
if assignee_ids?
::User.where(id: params[:assignee_ids])
else
super
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
def epics def epics
if params[:include_subepics] if params[:include_subepics]
::Gitlab::ObjectHierarchy.new(::Epic.id_in(params[:epic_id])).base_and_descendants.select(:id) ::Gitlab::ObjectHierarchy.new(::Epic.id_in(params[:epic_id])).base_and_descendants.select(:id)
......
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