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

Refactor Gitlab::SnippetSearchResults

This removes the need for plucking snippet IDs into memory.
parent e3e24e843bbd
......@@ -7,8 +7,9 @@ def initialize(user, params)
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
......@@ -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 @@ def snippet_blobs_count
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
......
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