BigW Consortium Gitlab

participable.rb 1.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# == Participable concern
#
# Contains functionality related to objects that can have participants, such as
# an author, an assignee and people mentioned in its description or comments.
#
# Used by Issue, Note, MergeRequest, Snippet and Commit.
#
# Usage:
#
#     class Issue < ActiveRecord::Base
#       include Participable
#
#       # ...
#
15
#       participant :author, :assignee, :notes, ->(current_user) { mentioned_users(current_user) }
16
#     end
17
#
18 19 20 21 22 23 24
#     issue = Issue.last
#     users = issue.participants
#     # `users` will contain the issue's author, its assignee,
#     # all users returned by its #mentioned_users method,
#     # as well as all participants to all of the issue's notes,
#     # since Note implements Participable as well.
#
25 26 27 28 29
module Participable
  extend ActiveSupport::Concern

  module ClassMethods
    def participant(*attrs)
30
      participant_attrs.concat(attrs)
31 32 33 34 35 36 37
    end

    def participant_attrs
      @participant_attrs ||= []
    end
  end

38 39
  # Be aware that this method makes a lot of sql queries.
  # Save result into variable if you are going to reuse it inside same request
40
  def participants(current_user = self.author)
41 42 43 44 45 46 47 48 49
    participants =
      Gitlab::ReferenceExtractor.lazily do
        self.class.participant_attrs.flat_map do |attr|
          value =
            if attr.respond_to?(:call)
              instance_exec(current_user, &attr)
            else
              send(attr)
            end
50

51 52 53
          participants_for(value, current_user)
        end.compact.uniq
      end
54

55
    unless Gitlab::ReferenceExtractor.lazy?
56 57
      participants.select! do |user|
        user.can?(:read_project, project)
58 59 60 61
      end
    end

    participants
62 63 64
  end

  private
65

66
  def participants_for(value, current_user = nil)
Douwe Maan committed
67
    case value
68
    when User, Banzai::LazyReference
Douwe Maan committed
69
      [value]
70
    when Enumerable, ActiveRecord::Relation
71
      value.flat_map { |v| participants_for(v, current_user) }
Douwe Maan committed
72
    when Participable
73
      value.participants(current_user)
74
    end
Douwe Maan committed
75
  end
76
end