diff options
author | Douwe Maan <douwe@gitlab.com> | 2015-10-14 21:29:35 +0200 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-10-14 21:29:35 +0200 |
commit | 925dc5925b672718ea1f0f08c5bdd492b5ab3e5d (patch) | |
tree | 22a58bbe8f2beba9d501667fc0ea274072741788 /app/models | |
parent | 539de0dd81ea0d831031c06da502254952d87676 (diff) | |
download | gitlab-ce-925dc5925b672718ea1f0f08c5bdd492b5ab3e5d.tar.gz |
Cache rendered contents of issues, MRs and notes
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/concerns/issuable.rb | 3 | ||||
-rw-r--r-- | app/models/concerns/mentionable.rb | 39 | ||||
-rw-r--r-- | app/models/note.rb | 2 |
3 files changed, 27 insertions, 17 deletions
diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index feee8460b86..e260eae8a43 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -46,7 +46,8 @@ module Issuable allow_nil: true, prefix: true - attr_mentionable :title, :description + attr_mentionable :title + attr_mentionable :description, cache: true participant :author, :assignee, :notes end diff --git a/app/models/concerns/mentionable.rb b/app/models/concerns/mentionable.rb index 715fc6f689d..ad432144bd9 100644 --- a/app/models/concerns/mentionable.rb +++ b/app/models/concerns/mentionable.rb @@ -10,8 +10,9 @@ module Mentionable module ClassMethods # Indicate which attributes of the Mentionable to search for GFM references. - def attr_mentionable(*attrs) - mentionable_attrs.concat(attrs.map(&:to_s)) + def attr_mentionable(attr, options = {}) + attr = attr.to_s + mentionable_attrs << [attr, options] end # Accessor for attributes marked mentionable. @@ -37,11 +38,6 @@ module Mentionable "#{friendly_name} #{to_reference(from_project)}" end - # Construct a String that contains possible GFM references. - def mentionable_text - self.class.mentionable_attrs.map { |attr| send(attr) }.compact.join("\n\n") - end - # The GFM reference to this Mentionable, which shouldn't be included in its #references. def local_reference self @@ -54,20 +50,33 @@ module Mentionable end def mentioned_users(current_user = nil, load_lazy_references: true) - return [] if mentionable_text.blank? - + # TODO: Douwe: Will be simplified when the "Simplify ..." MR is merged. ext = Gitlab::ReferenceExtractor.new(self.project, current_user, load_lazy_references: load_lazy_references) - ext.analyze(mentionable_text) - ext.users.uniq + self.class.mentionable_attrs.each do |attr, options| + text = send(attr) + cache_key = [self, attr] if options[:cache] + ext.analyze(text, cache_key: cache_key, pipeline: options[:pipeline]) + end + ext.users end # Extract GFM references to other Mentionables from this Mentionable. Always excludes its #local_reference. - def references(p = project, current_user = self.author, text = mentionable_text, load_lazy_references: true) + def references(p = project, current_user = self.author, text = nil, load_lazy_references: true) return [] if text.blank? ext = Gitlab::ReferenceExtractor.new(p, current_user, load_lazy_references: load_lazy_references) - ext.analyze(text) - (ext.issues + ext.merge_requests + ext.commits).uniq - [local_reference] + + if text + ext.analyze(text) + else + self.class.mentionable_attrs.each do |attr, options| + text = send(attr) + cache_key = [self, attr] if options[:cache] + ext.analyze(text, cache_key: cache_key) + end + end + + (ext.issues + ext.merge_requests + ext.commits) - [local_reference] end # Create a cross-reference Note for each GFM reference to another Mentionable found in +mentionable_text+. @@ -111,7 +120,7 @@ module Mentionable def detect_mentionable_changes source = (changes.present? ? changes : previous_changes).dup - mentionable = self.class.mentionable_attrs + mentionable = self.class.mentionable_attrs.map { |attr, options| attr } # Only include changed fields that are mentionable source.select { |key, val| mentionable.include?(key) } diff --git a/app/models/note.rb b/app/models/note.rb index 2fbe4784159..b4d6ddba703 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -28,7 +28,7 @@ class Note < ActiveRecord::Base default_value_for :system, false - attr_mentionable :note + attr_mentionable :note, cache: true, pipeline: :note participant :author belongs_to :project |