summaryrefslogtreecommitdiff
path: root/app/models/concerns/mentionable.rb
blob: f22070f85043f97b6eef5b10c4884eb8698c969e (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
# == Mentionable concern
#
# Contains common functionality shared between Issues and Notes
#
# Used by Issue, Note
#
module Mentionable
  extend ActiveSupport::Concern

  def mentioned_users
    users = []
    return users if mentionable_text.blank?
    has_project = self.respond_to? :project
    matches = mentionable_text.scan(/@[a-zA-Z][a-zA-Z0-9_\-\.]*/)
    matches.each do |match|
      identifier = match.delete "@"
      if has_project
        id = project.users_projects.joins(:user).where(users: { username: identifier }).pluck(:user_id).first
      else
        id = User.where(username: identifier).pluck(:id).first
      end
      users << User.find(id) unless id.blank?
    end
    users.uniq
  end

  def mentionable_text
    if self.class == Issue
      description
    elsif self.class == Note
      note
    else
      nil
    end
  end

end