2012-03-15 19:14:39 -04:00
|
|
|
class SearchController < ApplicationController
|
2017-02-01 13:21:28 -05:00
|
|
|
skip_before_action :authenticate_user!
|
2016-03-01 09:57:28 -05:00
|
|
|
|
2014-01-18 07:16:46 -05:00
|
|
|
include SearchHelper
|
|
|
|
|
2015-05-01 04:39:11 -04:00
|
|
|
layout 'search'
|
2015-04-30 13:06:18 -04:00
|
|
|
|
2012-03-15 19:14:39 -04:00
|
|
|
def show
|
2017-03-31 09:03:55 -04:00
|
|
|
search_service = SearchService.new(current_user, params)
|
2015-04-10 12:39:36 -04:00
|
|
|
|
2017-03-31 09:03:55 -04:00
|
|
|
@project = search_service.project
|
|
|
|
@group = search_service.group
|
2015-04-28 05:48:42 -04:00
|
|
|
|
2016-11-07 13:36:58 -05:00
|
|
|
return if params[:search].blank?
|
2016-09-09 07:08:49 -04:00
|
|
|
|
2016-04-19 03:52:15 -04:00
|
|
|
@search_term = params[:search]
|
|
|
|
|
2017-03-31 09:03:55 -04:00
|
|
|
@scope = search_service.scope
|
|
|
|
@show_snippets = search_service.show_snippets?
|
|
|
|
@search_results = search_service.search_results
|
|
|
|
@search_objects = search_service.search_objects
|
2017-01-10 23:20:32 -05:00
|
|
|
|
|
|
|
check_single_commit_result
|
2013-10-23 16:27:40 -04:00
|
|
|
end
|
2014-01-18 07:16:46 -05:00
|
|
|
|
|
|
|
def autocomplete
|
|
|
|
term = params[:term]
|
2015-04-10 12:39:10 -04:00
|
|
|
|
|
|
|
if params[:project_id].present?
|
|
|
|
@project = Project.find_by(id: params[:project_id])
|
|
|
|
@project = nil unless can?(current_user, :read_project, @project)
|
|
|
|
end
|
|
|
|
|
2014-01-18 07:16:46 -05:00
|
|
|
@ref = params[:project_ref] if params[:project_ref].present?
|
|
|
|
|
|
|
|
render json: search_autocomplete_opts(term).to_json
|
|
|
|
end
|
2017-01-10 23:20:32 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_single_commit_result
|
|
|
|
if @search_results.single_commit_result?
|
|
|
|
only_commit = @search_results.objects('commits').first
|
|
|
|
query = params[:search].strip.downcase
|
|
|
|
found_by_commit_sha = Commit.valid_hash?(query) && only_commit.sha.start_with?(query)
|
|
|
|
|
|
|
|
redirect_to namespace_project_commit_path(@project.namespace, @project, only_commit) if found_by_commit_sha
|
|
|
|
end
|
|
|
|
end
|
2012-03-15 19:14:39 -04:00
|
|
|
end
|