Commit e739eb03 authored by Douwe Maan's avatar Douwe Maan

Move participants method to shared Participable concern.

parent 0ff778c0
...@@ -3,8 +3,10 @@ class Commit ...@@ -3,8 +3,10 @@ class Commit
include StaticModel include StaticModel
extend ActiveModel::Naming extend ActiveModel::Naming
include Mentionable include Mentionable
include Participable
attr_mentionable :safe_message attr_mentionable :safe_message
participant :author, :committer, :notes, :mentioned_users
attr_accessor :project attr_accessor :project
...@@ -137,21 +139,6 @@ class Commit ...@@ -137,21 +139,6 @@ class Commit
User.find_for_commit(committer_email, committer_name) User.find_for_commit(committer_email, committer_name)
end end
def participants(current_user = nil)
users = []
users << author
users << committer
users.push *self.mentioned_users(current_user)
notes.each do |note|
users << note.author
users.push *note.mentioned_users(current_user)
end
users.uniq
end
def notes def notes
project.notes.for_commit_id(self.id) project.notes.for_commit_id(self.id)
end end
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
module Issuable module Issuable
extend ActiveSupport::Concern extend ActiveSupport::Concern
include Mentionable include Mentionable
include Participable
included do included do
belongs_to :author, class_name: "User" belongs_to :author, class_name: "User"
...@@ -45,6 +46,7 @@ module Issuable ...@@ -45,6 +46,7 @@ module Issuable
prefix: true prefix: true
attr_mentionable :title, :description attr_mentionable :title, :description
participant :author, :assignee, :notes, :mentioned_users
end end
module ClassMethods module ClassMethods
...@@ -117,22 +119,6 @@ module Issuable ...@@ -117,22 +119,6 @@ module Issuable
upvotes + downvotes upvotes + downvotes
end end
# Return all users participating on the discussion
def participants(current_user = self.author)
users = []
users << author
users << assignee if is_assigned?
users.push *self.mentioned_users(current_user)
notes.each do |note|
users << note.author
users.push *note.mentioned_users(current_user)
end
users.uniq
end
def subscribed?(user) def subscribed?(user)
subscription = subscriptions.find_by_user_id(user.id) subscription = subscriptions.find_by_user_id(user.id)
......
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 =
if meth.arity == 1
meth.call(current_user)
else
meth.call
end
participants_for(value, current_user)
end.compact.uniq
end
private
def participants_for(value, current_user = nil)
case value
when User
[value]
when Array
value.flat_map { |v| participants_for(v, current_user) }
when Participable
value.participants(current_user)
when Mentionable
value.mentioned_users(current_user)
end
end
end
...@@ -23,10 +23,12 @@ require 'file_size_validator' ...@@ -23,10 +23,12 @@ require 'file_size_validator'
class Note < ActiveRecord::Base class Note < ActiveRecord::Base
include Mentionable include Mentionable
include Gitlab::CurrentSettings include Gitlab::CurrentSettings
include Participable
default_value_for :system, false default_value_for :system, false
attr_mentionable :note attr_mentionable :note
participant :author, :mentioned_users
belongs_to :project belongs_to :project
belongs_to :noteable, polymorphic: true belongs_to :noteable, polymorphic: true
......
...@@ -19,6 +19,7 @@ class Snippet < ActiveRecord::Base ...@@ -19,6 +19,7 @@ class Snippet < ActiveRecord::Base
include Sortable include Sortable
include Linguist::BlobHelper include Linguist::BlobHelper
include Gitlab::VisibilityLevel include Gitlab::VisibilityLevel
include Participable
default_value_for :visibility_level, Snippet::PRIVATE default_value_for :visibility_level, Snippet::PRIVATE
...@@ -47,6 +48,8 @@ class Snippet < ActiveRecord::Base ...@@ -47,6 +48,8 @@ class Snippet < ActiveRecord::Base
scope :expired, -> { where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) } scope :expired, -> { where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) }
scope :non_expired, -> { where(["expires_at IS NULL OR expires_at > ?", Time.current]) } scope :non_expired, -> { where(["expires_at IS NULL OR expires_at > ?", Time.current]) }
participant :author, :notes
def self.content_types def self.content_types
[ [
".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java", ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
...@@ -87,18 +90,6 @@ class Snippet < ActiveRecord::Base ...@@ -87,18 +90,6 @@ class Snippet < ActiveRecord::Base
visibility_level visibility_level
end end
def participants(current_user = self.author)
users = []
users << author
notes.each do |note|
users << note.author
users.push *note.mentioned_users(current_user)
end
users.uniq
end
class << self class << self
def search(query) def search(query)
where('(title LIKE :query OR file_name LIKE :query)', query: "%#{query}%") where('(title LIKE :query OR file_name LIKE :query)', query: "%#{query}%")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment