summaryrefslogtreecommitdiff
path: root/app/controllers/concerns/issuable_collections.rb
blob: a6e158ebae6a7474e260ae132a5dbdeba5f675f6 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
module IssuableCollections
  extend ActiveSupport::Concern
  include SortingHelper

  included do
    helper_method :issues_finder
    helper_method :merge_requests_finder
  end

  private

  def issuable_meta_data(issuable_collection)
    # map has to be used here since using pluck or select will
    # throw an error when ordering issuables by priority which inserts
    # a new order into the collection.
    # We cannot use reorder to not mess up the paginated collection.
    issuable_ids         = issuable_collection.map(&:id)
    issuable_note_count  = Note.count_for_collection(issuable_ids, @collection_type)
    issuable_votes_count = AwardEmoji.votes_for_collection(issuable_ids, @collection_type)

    issuable_ids.each_with_object({}) do |id, issuable_meta|
      downvotes = issuable_votes_count.find { |votes| votes.awardable_id == id && votes.downvote? }
      upvotes   = issuable_votes_count.find { |votes| votes.awardable_id == id && votes.upvote? }
      notes     = issuable_note_count.find  { |notes| notes.noteable_id == id }

      issuable_meta[id] = Issuable::IssuableMeta.new(
        upvotes.try(:count).to_i,
        downvotes.try(:count).to_i,
        notes.try(:count).to_i
      )
    end
  end

  def issues_collection
    issues_finder.execute.preload(:project, :author, :assignee, :labels, :milestone, project: :namespace)
  end

  def merge_requests_collection
    merge_requests_finder.execute.preload(:source_project, :target_project, :author, :assignee, :labels, :milestone, :merge_request_diff, target_project: :namespace)
  end

  def issues_finder
    @issues_finder ||= issuable_finder_for(IssuesFinder)
  end

  def merge_requests_finder
    @merge_requests_finder ||= issuable_finder_for(MergeRequestsFinder)
  end

  def issuable_finder_for(finder_class)
    finder_class.new(current_user, filter_params)
  end

  def filter_params
    set_sort_order_from_cookie
    set_default_scope
    set_default_state

    @filter_params = params.dup
    @filter_params[:sort] ||= default_sort_order

    @sort = @filter_params[:sort]

    if @project
      @filter_params[:project_id] = @project.id
    elsif @group
      @filter_params[:group_id] = @group.id
    else
      # TODO: this filter ignore issues/mr created in public or
      # internal repos where you are not a member. Enable this filter
      # or improve current implementation to filter only issues you
      # created or assigned or mentioned
      # @filter_params[:authorized_only] = true
    end

    @filter_params
  end

  def set_default_scope
    params[:scope] = 'all' if params[:scope].blank?
  end

  def set_default_state
    params[:state] = 'opened' if params[:state].blank?
  end

  def set_sort_order_from_cookie
    key = 'issuable_sort'

    cookies[key] = params[:sort] if params[:sort].present?

    # id_desc and id_asc are old values for these two.
    cookies[key] = sort_value_recently_created if cookies[key] == 'id_desc'
    cookies[key] = sort_value_oldest_created if cookies[key] == 'id_asc'

    params[:sort] = cookies[key]
  end

  def default_sort_order
    case params[:state]
    when 'opened', 'all' then sort_value_recently_created
    when 'merged', 'closed' then sort_value_recently_updated
    else sort_value_recently_created
    end
  end
end