summaryrefslogtreecommitdiff
path: root/app/services/search/project_service.rb
blob: 3ebaafc752caf2cf1bd1d7d887f513c8555fe44b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Search
  class ProjectService
    attr_accessor :project, :current_user, :params

    def initialize(project, user, params)
      @project, @current_user, @params = project, user, params.dup
    end

    def execute
      query = params[:search]
      query = Shellwords.shellescape(query) if query.present?
      return result unless query.present?

      if params[:search_code].present?
        blobs = project.repository.search_files(query, params[:repository_ref]) unless project.empty_repo?
        blobs = Kaminari.paginate_array(blobs).page(params[:page]).per(20)
        result[:blobs] = blobs
        result[:total_results] = blobs.total_count
      else
        result[:merge_requests] = project.merge_requests.search(query).order('updated_at DESC').limit(20)
        result[:issues] = project.issues.search(query).order('updated_at DESC').limit(20)
        result[:total_results] = %w(issues merge_requests).sum { |items| result[items.to_sym].size }
      end

      result
    end

    def result
      @result ||= {
        merge_requests: [],
        issues: [],
        blobs: [],
        total_results: 0,
      }
    end
  end
end