summaryrefslogtreecommitdiff
path: root/app/finders/issuables/author_filter.rb
blob: f36daae553ddad70cdc7acf5e35b2226beeb6cf9 (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
# frozen_string_literal: true

module Issuables
  class AuthorFilter < BaseFilter
    def filter(issuables)
      filtered = by_author(issuables)
      filtered = by_author_union(filtered)
      by_negated_author(filtered)
    end

    private

    def by_author(issuables)
      if params[:author_id].present?
        issuables.authored(params[:author_id])
      elsif params[:author_username].present?
        issuables.authored(User.by_username(params[:author_username]))
      else
        issuables
      end
    end

    def by_author_union(issuables)
      return issuables unless or_filters_enabled? && or_params&.fetch(:author_username, false).present?

      issuables.authored(User.by_username(or_params[:author_username]))
    end

    def by_negated_author(issuables)
      return issuables unless not_params.present?

      if not_params[:author_id].present?
        issuables.not_authored(not_params[:author_id])
      elsif not_params[:author_username].present?
        issuables.not_authored(User.by_username(not_params[:author_username]))
      else
        issuables
      end
    end
  end
end