Welcome to mirror list, hosted at ThFree Co, Russian Federation.

assignee_filter.rb « issuables « finders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e58a6b34c9e9860aec82aafa0506b9191491235 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

module Issuables
  class AssigneeFilter < BaseFilter
    def filter(issuables)
      filtered = by_assignee(issuables)
      filtered = by_assignee_union(filtered)
      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_assignee_union(issuables)
      return issuables unless or_filters_enabled? && has_assignee_param?(or_params)

      issuables.assigned_to(assignee_ids(or_params))
    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