summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2016-03-01 18:01:09 +0100
committerRobert Speicher <rspeicher@gmail.com>2016-03-11 15:25:22 -0500
commit42fde69d39234368f8252febc9bf6ca3eca6f275 (patch)
treea0e1a9cf6808195653d4f8e383862a7500c6e5e5
parent013542965c2b6d84d28aab4823e5400897610087 (diff)
downloadgitlab-ce-42fde69d39234368f8252febc9bf6ca3eca6f275.tar.gz
Refactor Gitlab::SnippetSearchResults
This removes the need for plucking snippet IDs into memory.
-rw-r--r--app/services/search/snippet_service.rb5
-rw-r--r--lib/gitlab/snippet_search_results.rb10
-rw-r--r--spec/lib/gitlab/snippet_search_results_spec.rb25
3 files changed, 33 insertions, 7 deletions
diff --git a/app/services/search/snippet_service.rb b/app/services/search/snippet_service.rb
index 8ca0877321d..0b3e713e220 100644
--- a/app/services/search/snippet_service.rb
+++ b/app/services/search/snippet_service.rb
@@ -7,8 +7,9 @@ module Search
end
def execute
- snippet_ids = Snippet.accessible_to(current_user).pluck(:id)
- Gitlab::SnippetSearchResults.new(snippet_ids, params[:search])
+ snippets = Snippet.accessible_to(current_user)
+
+ Gitlab::SnippetSearchResults.new(snippets, params[:search])
end
end
end
diff --git a/lib/gitlab/snippet_search_results.rb b/lib/gitlab/snippet_search_results.rb
index addda95be2b..e0e74ff8359 100644
--- a/lib/gitlab/snippet_search_results.rb
+++ b/lib/gitlab/snippet_search_results.rb
@@ -2,10 +2,10 @@ module Gitlab
class SnippetSearchResults < SearchResults
include SnippetsHelper
- attr_reader :limit_snippet_ids
+ attr_reader :limit_snippets
- def initialize(limit_snippet_ids, query)
- @limit_snippet_ids = limit_snippet_ids
+ def initialize(limit_snippets, query)
+ @limit_snippets = limit_snippets
@query = query
end
@@ -35,11 +35,11 @@ module Gitlab
private
def snippet_titles
- Snippet.where(id: limit_snippet_ids).search(query).order('updated_at DESC')
+ limit_snippets.search(query).order('updated_at DESC')
end
def snippet_blobs
- Snippet.where(id: limit_snippet_ids).search_code(query).order('updated_at DESC')
+ limit_snippets.search_code(query).order('updated_at DESC')
end
def default_scope
diff --git a/spec/lib/gitlab/snippet_search_results_spec.rb b/spec/lib/gitlab/snippet_search_results_spec.rb
new file mode 100644
index 00000000000..e86b9ef6a63
--- /dev/null
+++ b/spec/lib/gitlab/snippet_search_results_spec.rb
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+describe Gitlab::SnippetSearchResults do
+ let!(:snippet) { create(:snippet, content: 'foo', file_name: 'foo') }
+
+ let(:results) { described_class.new(Snippet.all, 'foo') }
+
+ describe '#total_count' do
+ it 'returns the total amount of search hits' do
+ expect(results.total_count).to eq(2)
+ end
+ end
+
+ describe '#snippet_titles_count' do
+ it 'returns the amount of matched snippet titles' do
+ expect(results.snippet_titles_count).to eq(1)
+ end
+ end
+
+ describe '#snippet_blobs_count' do
+ it 'returns the amount of matched snippet blobs' do
+ expect(results.snippet_blobs_count).to eq(1)
+ end
+ end
+end