diff options
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/concerns/issuable.rb | 15 | ||||
| -rw-r--r-- | app/models/concerns/mentionable.rb | 37 | ||||
| -rw-r--r-- | app/models/note.rb | 2 |
3 files changed, 54 insertions, 0 deletions
diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index cb238c15ed8..8868e818daa 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -6,6 +6,7 @@ # module Issuable extend ActiveSupport::Concern + include Mentionable included do belongs_to :project @@ -96,4 +97,18 @@ module Issuable def votes_count upvotes + downvotes end + + # Return all users participating on the discussion + def participants + users = [] + users << author + users << assignee if is_assigned? + mentions = [] + mentions << self.mentioned_users + notes.each do |note| + users << note.author + mentions << note.mentioned_users + end + users.concat(mentions.reduce([], :|)).uniq + end end diff --git a/app/models/concerns/mentionable.rb b/app/models/concerns/mentionable.rb new file mode 100644 index 00000000000..f22070f8504 --- /dev/null +++ b/app/models/concerns/mentionable.rb @@ -0,0 +1,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 diff --git a/app/models/note.rb b/app/models/note.rb index c2e664d1bcd..c23aab03bcc 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -19,6 +19,8 @@ require 'carrierwave/orm/activerecord' require 'file_size_validator' class Note < ActiveRecord::Base + include Mentionable + attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id, :attachment, :line_code, :commit_id |
