Commit 35e977d6 authored by Yorick Peterse's avatar Yorick Peterse

Refactor Mentionable

Caching is now always enabled when rendering attributes as there's no
reason to not cache them. The method Mentionable#all_references has also
been modified to take an optional Gitlab::ReferenceExtractor instance to
allow other code to create one and re-use it between multiple calls.
parent 25c08d11
...@@ -23,7 +23,7 @@ module Mentionable ...@@ -23,7 +23,7 @@ module Mentionable
included do included do
if self < Participable if self < Participable
participant ->(current_user) { mentioned_users(current_user) } participant -> (user, ext) { all_references(user, extractor: ext) }
end end
end end
...@@ -43,23 +43,22 @@ module Mentionable ...@@ -43,23 +43,22 @@ module Mentionable
self self
end end
def all_references(current_user = nil, text = nil) def all_references(current_user = nil, text = nil, extractor: nil)
ext = Gitlab::ReferenceExtractor.new(self.project, current_user || self.author, self.author) extractor ||= Gitlab::ReferenceExtractor.
new(project, current_user || author)
if text if text
ext.analyze(text) extractor.analyze(text, author: author)
else else
self.class.mentionable_attrs.each do |attr, options| self.class.mentionable_attrs.each do |attr, options|
text = send(attr) text = __send__(attr)
options = options.merge(cache_key: [self, attr], author: author)
context = options.dup extractor.analyze(text, options)
context[:cache_key] = [self, attr] if context.delete(:cache) && self.persisted?
ext.analyze(text, context)
end end
end end
ext extractor
end end
def mentioned_users(current_user = nil) def mentioned_users(current_user = nil)
......
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