BigW Consortium Gitlab

search_controller.rb 1.68 KB
Newer Older
1
class SearchController < ApplicationController
2 3
  include SearchHelper

4
  layout 'search'
5

6
  def show
7
    return if params[:search].nil? || params[:search].blank?
8

9 10
    @search_term = params[:search]

11 12 13 14 15 16
    if params[:project_id].present?
      @project = Project.find_by(id: params[:project_id])
      @project = nil unless can?(current_user, :download_code, @project)
    end

    if params[:group_id].present?
17
      @group = Group.find_by(id: params[:group_id])
18 19
      @group = nil unless can?(current_user, :read_group, @group)
    end
20

21
    @scope = params[:scope]
22
    @show_snippets = params[:snippets].eql? 'true'
23

24
    @search_results =
25
      if @project
26 27
        unless %w(blobs notes issues merge_requests milestones wiki_blobs
                  commits).include?(@scope)
28 29 30 31 32 33 34 35 36 37 38
          @scope = 'blobs'
        end

        Search::ProjectService.new(@project, current_user, params).execute
      elsif @show_snippets
        unless %w(snippet_blobs snippet_titles).include?(@scope)
          @scope = 'snippet_blobs'
        end

        Search::SnippetService.new(current_user, params).execute
      else
39
        unless %w(projects issues merge_requests milestones).include?(@scope)
40 41 42 43
          @scope = 'projects'
        end
        Search::GlobalService.new(current_user, params).execute
      end
44

45
    @objects = @search_results.objects(@scope, params[:page])
46
  end
47 48 49

  def autocomplete
    term = params[:term]
50 51 52 53 54 55

    if params[:project_id].present?
      @project = Project.find_by(id: params[:project_id])
      @project = nil unless can?(current_user, :read_project, @project)
    end

56 57 58 59
    @ref = params[:project_ref] if params[:project_ref].present?

    render json: search_autocomplete_opts(term).to_json
  end
60
end