gitlab-org--gitlab-foss/app/services/search_service.rb

76 lines
1.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-03-31 09:03:55 -04:00
class SearchService
include Gitlab::Allowable
def initialize(current_user, params = {})
@current_user = current_user
@params = params.dup
end
# rubocop: disable CodeReuse/ActiveRecord
2017-03-31 09:03:55 -04:00
def project
return @project if defined?(@project)
@project =
if params[:project_id].present?
the_project = Project.find_by(id: params[:project_id])
can?(current_user, :read_project, the_project) ? the_project : nil
2017-03-31 09:03:55 -04:00
else
nil
end
end
# rubocop: enable CodeReuse/ActiveRecord
2017-03-31 09:03:55 -04:00
# rubocop: disable CodeReuse/ActiveRecord
2017-03-31 09:03:55 -04:00
def group
return @group if defined?(@group)
@group =
if params[:group_id].present?
the_group = Group.find_by(id: params[:group_id])
can?(current_user, :read_group, the_group) ? the_group : nil
else
nil
end
end
# rubocop: enable CodeReuse/ActiveRecord
2017-03-31 09:03:55 -04:00
def show_snippets?
return @show_snippets if defined?(@show_snippets)
@show_snippets = params[:snippets] == 'true'
end
delegate :scope, to: :search_service
def search_results
@search_results ||= search_service.execute
end
def search_objects
@search_objects ||= search_results.objects(scope, params[:page])
end
def display_options
@display_options ||= search_results.display_options(scope)
end
2017-03-31 09:03:55 -04:00
private
def search_service
@search_service ||=
if project
Search::ProjectService.new(project, current_user, params)
elsif show_snippets?
Search::SnippetService.new(current_user, params)
elsif group
Search::GroupService.new(current_user, group, params)
2017-03-31 09:03:55 -04:00
else
Search::GlobalService.new(current_user, params)
end
end
attr_reader :current_user, :params
end