Commit 42fde69d authored by Yorick Peterse's avatar Yorick Peterse Committed by Robert Speicher

Refactor Gitlab::SnippetSearchResults

This removes the need for plucking snippet IDs into memory.
parent 01354296
...@@ -7,8 +7,9 @@ module Search ...@@ -7,8 +7,9 @@ module Search
end end
def execute def execute
snippet_ids = Snippet.accessible_to(current_user).pluck(:id) snippets = Snippet.accessible_to(current_user)
Gitlab::SnippetSearchResults.new(snippet_ids, params[:search])
Gitlab::SnippetSearchResults.new(snippets, params[:search])
end end
end end
end end
...@@ -2,10 +2,10 @@ module Gitlab ...@@ -2,10 +2,10 @@ module Gitlab
class SnippetSearchResults < SearchResults class SnippetSearchResults < SearchResults
include SnippetsHelper include SnippetsHelper
attr_reader :limit_snippet_ids attr_reader :limit_snippets
def initialize(limit_snippet_ids, query) def initialize(limit_snippets, query)
@limit_snippet_ids = limit_snippet_ids @limit_snippets = limit_snippets
@query = query @query = query
end end
...@@ -35,11 +35,11 @@ module Gitlab ...@@ -35,11 +35,11 @@ module Gitlab
private private
def snippet_titles def snippet_titles
Snippet.where(id: limit_snippet_ids).search(query).order('updated_at DESC') limit_snippets.search(query).order('updated_at DESC')
end end
def snippet_blobs 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 end
def default_scope def default_scope
......
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment