BigW Consortium Gitlab

search_controller.rb 1.75 KB
Newer Older
1
class SearchController < ApplicationController
2
  skip_before_action :authenticate_user!, :reject_blocked!
3

4 5
  include SearchHelper

6
  layout 'search'
7

8
  def show
9 10
    return if params[:search].nil? || params[:search].blank?

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 22
    @search_term = params[:search]

23
    @scope = params[:scope]
24
    @show_snippets = params[:snippets].eql? 'true'
25

26
    @search_results =
27
      if @project
28 29
        unless %w(blobs notes issues merge_requests milestones wiki_blobs
                  commits).include?(@scope)
30 31 32 33 34 35 36 37 38 39 40
          @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
41
        unless %w(projects issues merge_requests milestones).include?(@scope)
42 43 44 45
          @scope = 'projects'
        end
        Search::GlobalService.new(current_user, params).execute
      end
46

47
    @search_objects = @search_results.objects(@scope, params[:page])
48
  end
49 50 51

  def autocomplete
    term = params[:term]
52 53 54 55 56 57

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

58 59 60 61
    @ref = params[:project_ref] if params[:project_ref].present?

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