2015-04-23 15:27:45 +00:00
|
|
|
# == 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
|
|
|
|
#
|
|
|
|
# # ...
|
|
|
|
#
|
|
|
|
# participant :author, :assignee, :mentioned_users, :notes
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2015-04-21 13:23:20 +00:00
|
|
|
module Participable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def participant(*attrs)
|
|
|
|
participant_attrs.concat(attrs.map(&:to_s))
|
|
|
|
end
|
|
|
|
|
|
|
|
def participant_attrs
|
|
|
|
@participant_attrs ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def participants(current_user = self.author)
|
|
|
|
self.class.participant_attrs.flat_map do |attr|
|
|
|
|
meth = method(attr)
|
|
|
|
|
|
|
|
value =
|
2015-05-14 11:15:13 +00:00
|
|
|
if meth.arity == 1 || meth.arity == -1
|
2015-04-21 13:23:20 +00:00
|
|
|
meth.call(current_user)
|
|
|
|
else
|
|
|
|
meth.call
|
|
|
|
end
|
|
|
|
|
|
|
|
participants_for(value, current_user)
|
|
|
|
end.compact.uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2015-04-21 15:41:18 +00:00
|
|
|
|
|
|
|
def participants_for(value, current_user = nil)
|
|
|
|
case value
|
|
|
|
when User
|
|
|
|
[value]
|
2015-04-23 15:27:03 +00:00
|
|
|
when Enumerable, ActiveRecord::Relation
|
2015-04-21 15:41:18 +00:00
|
|
|
value.flat_map { |v| participants_for(v, current_user) }
|
|
|
|
when Participable
|
|
|
|
value.participants(current_user)
|
2015-04-21 13:23:20 +00:00
|
|
|
end
|
2015-04-21 15:41:18 +00:00
|
|
|
end
|
2015-04-21 13:23:20 +00:00
|
|
|
end
|