summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaco Guzman <pacoguzmanp@gmail.com>2016-06-20 17:49:15 +0200
committerPaco Guzman <pacoguzmanp@gmail.com>2016-06-20 18:22:50 +0200
commit1b969e5e3220488f10f475bd36886c5d0e9c99f3 (patch)
tree1915ee31de28e7640108103d3332dda19dee463d
parent699badabc9670e836eafaf1c66248f49ea41a098 (diff)
downloadgitlab-ce-18792-cache-participants-call.tar.gz
Cache Participable#participants in instance variable18792-cache-participants-call
-rw-r--r--CHANGELOG1
-rw-r--r--app/models/concerns/participable.rb10
-rw-r--r--spec/models/concerns/participable_spec.rb10
3 files changed, 21 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 01e7e7a1606..35b709b1c31 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -129,6 +129,7 @@ v 8.9.0 (unreleased)
- Various associations are now eager loaded when parsing issue references to reduce the number of queries executed
- Set inverse_of for Project/Service association to reduce the number of queries
- Update tanuki logo highlight/loading colors
+ - Cache participable participants in an instance variable.
- Use Git cached counters for branches and tags on project page
- Filter parameters for request_uri value on instrumented transactions.
- Remove duplicated keys add UNIQUE index to keys fingerprint column
diff --git a/app/models/concerns/participable.rb b/app/models/concerns/participable.rb
index 9056722f45e..ec85d6b9e25 100644
--- a/app/models/concerns/participable.rb
+++ b/app/models/concerns/participable.rb
@@ -53,6 +53,16 @@ module Participable
#
# Returns an Array of User instances.
def participants(current_user = nil)
+ key = current_user ? current_user.id : nil
+
+ @participants = {} unless defined? @participants
+ @participants[key] = raw_participants(current_user) unless @participants.key?(key)
+ @participants[key]
+ end
+
+ private
+
+ def raw_participants(current_user = nil)
current_user ||= author
ext = Gitlab::ReferenceExtractor.new(project, current_user)
participants = Set.new
diff --git a/spec/models/concerns/participable_spec.rb b/spec/models/concerns/participable_spec.rb
index 7e4ea0f2d66..a9f4ef9ee5e 100644
--- a/spec/models/concerns/participable_spec.rb
+++ b/spec/models/concerns/participable_spec.rb
@@ -37,6 +37,16 @@ describe Participable, models: true do
expect(participants).to include(user3)
end
+ it 'caches the raw list of participants' do
+ instance = model.new
+ user1 = build(:user)
+
+ expect(instance).to receive(:raw_participants).once
+
+ instance.participants(user1)
+ instance.participants(user1)
+ end
+
it 'supports attributes returning another Participable' do
other_model = Class.new { include Participable }