summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormicael.bergeron <micaelbergeron@gmail.com>2017-11-21 08:31:23 -0500
committermicael.bergeron <micaelbergeron@gmail.com>2017-11-21 08:31:23 -0500
commitc900c21eef9235306d7d0da42b07aa2de346e263 (patch)
tree534592b7361aaa4160757be969e2435eb8c34679
parent9ed91479a7bbca1e420cd91a6322493d7ffda749 (diff)
downloadgitlab-ce-c900c21eef9235306d7d0da42b07aa2de346e263.tar.gz
add `#with_metadata` scope to remove a N+1 from the notes' API
-rw-r--r--app/models/note.rb1
-rw-r--r--lib/api/notes.rb4
2 files changed, 3 insertions, 2 deletions
diff --git a/app/models/note.rb b/app/models/note.rb
index 4bbb54ba9bf..50c9caf8529 100644
--- a/app/models/note.rb
+++ b/app/models/note.rb
@@ -110,6 +110,7 @@ class Note < ActiveRecord::Base
includes(:author, :noteable, :updated_by,
project: [:project_members, { group: [:group_members] }])
end
+ scope :with_metadata, -> { includes(:system_note_metadata) }
after_initialize :ensure_discussion_id
before_validation :nullify_blank_type, :nullify_blank_line_code
diff --git a/lib/api/notes.rb b/lib/api/notes.rb
index 0b9ab4eeb05..ceaaeca4046 100644
--- a/lib/api/notes.rb
+++ b/lib/api/notes.rb
@@ -33,7 +33,7 @@ module API
# paginate() only works with a relation. This could lead to a
# mismatch between the pagination headers info and the actual notes
# array returned, but this is really a edge-case.
- paginate(noteable.notes)
+ paginate(noteable.notes.with_metadata)
.reject { |n| n.cross_reference_not_visible_for?(current_user) }
present notes, with: Entities::Note
else
@@ -50,7 +50,7 @@ module API
end
get ":id/#{noteables_str}/:noteable_id/notes/:note_id" do
noteable = find_project_noteable(noteables_str, params[:noteable_id])
- note = noteable.notes.find(params[:note_id])
+ note = noteable.notes.with_metadata.find(params[:note_id])
can_read_note = can?(current_user, noteable_read_ability_name(noteable), noteable) && !note.cross_reference_not_visible_for?(current_user)
if can_read_note