2014-08-26 16:32:41 -04:00
|
|
|
module Gitlab
|
|
|
|
class SearchResults
|
2016-03-17 16:48:19 -04:00
|
|
|
attr_reader :current_user, :query
|
2014-08-26 16:32:41 -04:00
|
|
|
|
2016-03-01 11:56:14 -05:00
|
|
|
# Limit search results by passed projects
|
2014-08-26 16:32:41 -04:00
|
|
|
# It allows us to search only for projects user has access to
|
2016-03-01 11:56:14 -05:00
|
|
|
attr_reader :limit_projects
|
2014-08-26 16:32:41 -04:00
|
|
|
|
2016-03-17 16:48:19 -04:00
|
|
|
def initialize(current_user, limit_projects, query)
|
|
|
|
@current_user = current_user
|
2016-03-01 11:56:14 -05:00
|
|
|
@limit_projects = limit_projects || Project.all
|
2014-08-26 16:32:41 -04:00
|
|
|
@query = Shellwords.shellescape(query) if query.present?
|
|
|
|
end
|
|
|
|
|
2014-08-27 08:26:35 -04:00
|
|
|
def objects(scope, page = nil)
|
2014-08-26 16:32:41 -04:00
|
|
|
case scope
|
|
|
|
when 'projects'
|
|
|
|
projects.page(page).per(per_page)
|
|
|
|
when 'issues'
|
|
|
|
issues.page(page).per(per_page)
|
|
|
|
when 'merge_requests'
|
|
|
|
merge_requests.page(page).per(per_page)
|
2015-08-13 10:48:21 -04:00
|
|
|
when 'milestones'
|
|
|
|
milestones.page(page).per(per_page)
|
2014-08-26 16:32:41 -04:00
|
|
|
else
|
|
|
|
Kaminari.paginate_array([]).page(page).per(per_page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def projects_count
|
|
|
|
@projects_count ||= projects.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def issues_count
|
|
|
|
@issues_count ||= issues.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_requests_count
|
|
|
|
@merge_requests_count ||= merge_requests.count
|
|
|
|
end
|
|
|
|
|
2015-08-13 10:48:21 -04:00
|
|
|
def milestones_count
|
|
|
|
@milestones_count ||= milestones.count
|
|
|
|
end
|
|
|
|
|
2017-01-10 23:20:32 -05:00
|
|
|
def single_commit_result?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2014-08-26 16:32:41 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def projects
|
2016-03-01 11:56:14 -05:00
|
|
|
limit_projects.search(query)
|
2014-08-26 16:32:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def issues
|
2016-11-22 05:25:04 -05:00
|
|
|
issues = IssuesFinder.new(current_user).execute.where(project_id: project_ids_relation)
|
2016-03-01 11:56:14 -05:00
|
|
|
|
2015-05-17 13:32:58 -04:00
|
|
|
if query =~ /#(\d+)\z/
|
|
|
|
issues = issues.where(iid: $1)
|
|
|
|
else
|
|
|
|
issues = issues.full_search(query)
|
|
|
|
end
|
2016-03-01 11:56:14 -05:00
|
|
|
|
2015-05-17 13:32:58 -04:00
|
|
|
issues.order('updated_at DESC')
|
2014-08-26 16:32:41 -04:00
|
|
|
end
|
|
|
|
|
2015-08-13 10:48:21 -04:00
|
|
|
def milestones
|
2016-03-01 11:56:14 -05:00
|
|
|
milestones = Milestone.where(project_id: project_ids_relation)
|
2015-08-13 10:48:21 -04:00
|
|
|
milestones = milestones.search(query)
|
|
|
|
milestones.order('updated_at DESC')
|
|
|
|
end
|
|
|
|
|
2014-08-26 16:32:41 -04:00
|
|
|
def merge_requests
|
2016-11-29 08:47:43 -05:00
|
|
|
merge_requests = MergeRequestsFinder.new(current_user).execute.in_projects(project_ids_relation)
|
2015-05-17 13:32:58 -04:00
|
|
|
if query =~ /[#!](\d+)\z/
|
|
|
|
merge_requests = merge_requests.where(iid: $1)
|
|
|
|
else
|
|
|
|
merge_requests = merge_requests.full_search(query)
|
|
|
|
end
|
|
|
|
merge_requests.order('updated_at DESC')
|
2014-08-26 16:32:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_scope
|
|
|
|
'projects'
|
|
|
|
end
|
|
|
|
|
|
|
|
def per_page
|
|
|
|
20
|
|
|
|
end
|
2016-03-01 11:56:14 -05:00
|
|
|
|
|
|
|
def project_ids_relation
|
2016-03-03 12:35:17 -05:00
|
|
|
limit_projects.select(:id).reorder(nil)
|
2016-03-01 11:56:14 -05:00
|
|
|
end
|
2014-08-26 16:32:41 -04:00
|
|
|
end
|
|
|
|
end
|