summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Artur <felipefac@gmail.com>2017-05-31 18:12:27 -0300
committerFelipe Artur <felipefac@gmail.com>2017-06-01 12:51:53 -0300
commit80396341570ddae6e06361d9983acdd4bb4f0b85 (patch)
tree244d7cc6ca9d7866c314776fe76294b8da9d81a4
parentaff097e8f528ab5b00842df7c76d3435b1e59c96 (diff)
downloadgitlab-ce-issue_27166.tar.gz
Remove repeated query for merge requests discussion collectionissue_27166
-rw-r--r--app/models/note.rb8
-rw-r--r--spec/features/merge_requests/discussion_spec.rb21
2 files changed, 19 insertions, 10 deletions
diff --git a/app/models/note.rb b/app/models/note.rb
index 832c68243fb..115a2775ffb 100644
--- a/app/models/note.rb
+++ b/app/models/note.rb
@@ -249,7 +249,7 @@ class Note < ActiveRecord::Base
# When commit notes are rendered on an MR's Discussion page, they are
# displayed in one discussion instead of individually.
# See also `#discussion_id` and `Discussion.override_discussion_id`.
- if noteable && noteable != self.noteable
+ if different_context?(noteable)
OutOfContextDiscussion
else
IndividualNoteDiscussion
@@ -300,6 +300,12 @@ class Note < ActiveRecord::Base
private
+ def different_context?(noteable)
+ return false unless noteable
+
+ noteable.id != noteable_id && noteable.class.name != noteable_type
+ end
+
def keep_around_commit
project.repository.keep_around(self.commit_id)
end
diff --git a/spec/features/merge_requests/discussion_spec.rb b/spec/features/merge_requests/discussion_spec.rb
index 1a09cc54c2e..b2b133b0466 100644
--- a/spec/features/merge_requests/discussion_spec.rb
+++ b/spec/features/merge_requests/discussion_spec.rb
@@ -24,23 +24,26 @@ feature 'Merge Request Discussions', feature: true do
)
end
- let(:outdated_diff_refs) { project.commit("874797c3a73b60d2187ed6e2fcabd289ff75171e").diff_refs }
-
- before(:each) do
+ def visit_merge_request
visit namespace_project_merge_request_path(project.namespace, project, merge_request)
end
- context 'active discussions' do
- it 'shows a link to the diff' do
- within(".discussion[data-discussion-id='#{active_discussion.id}']") do
- path = diffs_namespace_project_merge_request_path(project.namespace, project, merge_request, anchor: active_discussion.line_code)
- expect(page).to have_link('the diff', href: path)
- end
+ let(:outdated_diff_refs) { project.commit("874797c3a73b60d2187ed6e2fcabd289ff75171e").diff_refs }
+
+ it "avoids repeated database queries" do
+ control_count = ActiveRecord::QueryRecorder.new { visit_merge_request }.count
+
+ 5.times do
+ create(:diff_note_on_merge_request, noteable: merge_request, project: project).to_discussion
end
+
+ expect { visit_merge_request }.not_to exceed_query_limit(control_count)
end
context 'outdated discussions' do
it 'shows a link to the outdated diff' do
+ visit_merge_request
+
within(".discussion[data-discussion-id='#{outdated_discussion.id}']") do
path = diffs_namespace_project_merge_request_path(project.namespace, project, merge_request, diff_id: old_merge_request_diff.id, anchor: outdated_discussion.line_code)
expect(page).to have_link('an old version of the diff', href: path)