debian-mirror-gitlab/lib/gitlab/snippet_search_results.rb

78 lines
1.6 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
module Gitlab
class SnippetSearchResults < SearchResults
2016-04-02 18:10:28 +05:30
include SnippetsHelper
2019-12-21 20:55:43 +05:30
attr_reader :current_user
2015-04-26 12:48:37 +05:30
2019-12-21 20:55:43 +05:30
def initialize(current_user, query)
@current_user = current_user
2015-04-26 12:48:37 +05:30
@query = query
end
def objects(scope, page = nil)
case scope
when 'snippet_titles'
2019-12-21 20:55:43 +05:30
paginated_objects(snippet_titles, page)
2015-04-26 12:48:37 +05:30
when 'snippet_blobs'
2019-12-21 20:55:43 +05:30
paginated_objects(snippet_blobs, page)
2015-04-26 12:48:37 +05:30
else
2018-03-17 18:26:18 +05:30
super(scope, nil, false)
2015-04-26 12:48:37 +05:30
end
end
2019-10-12 21:52:04 +05:30
def formatted_count(scope)
case scope
when 'snippet_titles'
2019-12-21 20:55:43 +05:30
formatted_limited_count(limited_snippet_titles_count)
2019-10-12 21:52:04 +05:30
when 'snippet_blobs'
2019-12-21 20:55:43 +05:30
formatted_limited_count(limited_snippet_blobs_count)
2019-10-12 21:52:04 +05:30
else
super
end
end
2019-12-21 20:55:43 +05:30
def limited_snippet_titles_count
@limited_snippet_titles_count ||= limited_count(snippet_titles)
2015-04-26 12:48:37 +05:30
end
2019-12-21 20:55:43 +05:30
def limited_snippet_blobs_count
@limited_snippet_blobs_count ||= limited_count(snippet_blobs)
2015-04-26 12:48:37 +05:30
end
private
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2019-12-21 20:55:43 +05:30
def snippets
SnippetsFinder.new(current_user, finder_params)
.execute
.includes(:author)
.reorder(updated_at: :desc)
2015-04-26 12:48:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
2019-12-21 20:55:43 +05:30
def snippet_titles
snippets.search(query)
end
2015-04-26 12:48:37 +05:30
def snippet_blobs
2019-12-21 20:55:43 +05:30
snippets.search_code(query)
2015-04-26 12:48:37 +05:30
end
def default_scope
'snippet_blobs'
end
2019-12-21 20:55:43 +05:30
def paginated_objects(relation, page)
relation.page(page).per(per_page)
end
def finder_params
{}
end
2015-04-26 12:48:37 +05:30
end
end
2019-12-21 20:55:43 +05:30
Gitlab::SnippetSearchResults.prepend_if_ee('::EE::Gitlab::SnippetSearchResults')