Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-08-27 00:32:41 +0400
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-08-27 00:32:41 +0400
commit9a4ef7e7eb1fe73938578d82c2662913e3d51ad6 (patch)
tree77cddbb48950c62e4548911c46ae674f1845b0ed /lib/gitlab/project_search_results.rb
parentb3b50bb86ea89c54d790516552e65eb8b4149fe1 (diff)
Search results libraries added
Gitlab::SearchResults and Gitlab::ProjectSearchResults are libraries we are going to use to get search results based on query, enitity type and pagination. It will allow us to get only issues from project #23 where title or description includes 'foo'. Ex: search_results = Gitlab::ProjectSearchResults.new(project.id, 'foo', 'issues') search_results.objects => # [<Issues #23>, <Issues #34>] search_results.issues_count => 2 search_results.total_count => 12 (it includes results from comments and merge requests too) Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'lib/gitlab/project_search_results.rb')
-rw-r--r--lib/gitlab/project_search_results.rb62
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/gitlab/project_search_results.rb b/lib/gitlab/project_search_results.rb
new file mode 100644
index 00000000000..b0a0bee1c81
--- /dev/null
+++ b/lib/gitlab/project_search_results.rb
@@ -0,0 +1,62 @@
+module Gitlab
+ class ProjectSearchResults < SearchResults
+ attr_reader :project, :repository_ref
+
+ def initialize(project_id, query, scope = nil, page = nil, repository_ref = nil)
+ @project = Project.find(project_id)
+ @repository_ref = repository_ref
+ @page = page
+ @query = Shellwords.shellescape(query) if query.present?
+ @scope = scope
+
+ unless %w(blobs notes issues merge_requests).include?(@scope)
+ @scope = default_scope
+ end
+ end
+
+ def objects
+ case scope
+ when 'notes'
+ notes.page(page).per(per_page)
+ when 'blobs'
+ Kaminari.paginate_array(blobs).page(page).per(per_page)
+ else
+ super
+ end
+ end
+
+ def total_count
+ @total_count ||= issues_count + merge_requests_count + blobs_count + notes_count
+ end
+
+ def blobs_count
+ @blobs_count ||= blobs.count
+ end
+
+ def notes_count
+ @notes_count ||= notes.count
+ end
+
+ private
+
+ def blobs
+ if project.empty_repo?
+ []
+ else
+ project.repository.search_files(query, repository_ref)
+ end
+ end
+
+ def notes
+ Note.where(project_id: limit_project_ids).search(query).order('updated_at DESC')
+ end
+
+ def default_scope
+ 'blobs'
+ end
+
+ def limit_project_ids
+ [project.id]
+ end
+ end
+end