debian-mirror-gitlab/lib/gitlab/issuable_metadata.rb

47 lines
1.8 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Gitlab
module IssuableMetadata
2019-07-07 11:18:12 +05:30
def issuable_meta_data(issuable_collection, collection_type, user = nil)
2018-03-17 18:26:18 +05:30
# ActiveRecord uses Object#extend for null relations.
if !(issuable_collection.singleton_class < ActiveRecord::NullRelation) &&
issuable_collection.respond_to?(:limit_value) &&
issuable_collection.limit_value.nil?
raise 'Collection must have a limit applied for preloading meta-data'
end
2017-09-10 17:25:29 +05:30
# 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)
return {} if issuable_ids.empty?
2019-12-04 20:38:33 +05:30
issuable_notes_count = ::Note.count_for_collection(issuable_ids, collection_type)
2017-09-10 17:25:29 +05:30
issuable_votes_count = ::AwardEmoji.votes_for_collection(issuable_ids, collection_type)
issuable_merge_requests_count =
if collection_type == 'Issue'
2019-07-07 11:18:12 +05:30
::MergeRequestsClosingIssues.count_for_collection(issuable_ids, user)
2017-09-10 17:25:29 +05:30
else
[]
end
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? }
2019-12-04 20:38:33 +05:30
notes = issuable_notes_count.find { |notes| notes.noteable_id == id }
2017-09-10 17:25:29 +05:30
merge_requests = issuable_merge_requests_count.find { |mr| mr.first == id }
issuable_meta[id] = ::Issuable::IssuableMeta.new(
upvotes.try(:count).to_i,
downvotes.try(:count).to_i,
notes.try(:count).to_i,
merge_requests.try(:last).to_i
)
end
end
end
end