BigW Consortium Gitlab

project_search_results.rb 2.03 KB
Newer Older
1 2 3 4
module Gitlab
  class ProjectSearchResults < SearchResults
    attr_reader :project, :repository_ref

5
    def initialize(project_id, query, repository_ref = nil)
6
      @project = Project.find(project_id)
7 8 9 10 11
      @repository_ref = if repository_ref.present?
                          repository_ref
                        else
                          nil
                        end
12
      @query = query
13 14
    end

Dmitriy Zaporozhets committed
15
    def objects(scope, page = nil)
16 17 18 19 20
      case scope
      when 'notes'
        notes.page(page).per(per_page)
      when 'blobs'
        Kaminari.paginate_array(blobs).page(page).per(per_page)
Ralf Seidler committed
21 22
      when 'wiki_blobs'
        Kaminari.paginate_array(wiki_blobs).page(page).per(per_page)
23 24
      when 'commits'
        Kaminari.paginate_array(commits).page(page).per(per_page)
25 26 27 28 29 30
      else
        super
      end
    end

    def total_count
31
      @total_count ||= issues_count + merge_requests_count + blobs_count +
32
                       notes_count + wiki_blobs_count + commits_count
33 34 35 36 37 38 39 40 41 42
    end

    def blobs_count
      @blobs_count ||= blobs.count
    end

    def notes_count
      @notes_count ||= notes.count
    end

Ralf Seidler committed
43 44 45 46
    def wiki_blobs_count
      @wiki_blobs_count ||= wiki_blobs.count
    end

47 48 49 50
    def commits_count
      @commits_count ||= commits.count
    end

51 52 53
    private

    def blobs
54
      if project.empty_repo? || query.blank?
55 56 57 58 59 60
        []
      else
        project.repository.search_files(query, repository_ref)
      end
    end

Ralf Seidler committed
61
    def wiki_blobs
62
      if project.wiki_enabled? && query.present?
Dmitriy Zaporozhets committed
63
        project_wiki = ProjectWiki.new(project)
64

Dmitriy Zaporozhets committed
65 66
        unless project_wiki.empty?
          project_wiki.search_files(query)
67 68 69
        else
          []
        end
Ralf Seidler committed
70
      else
71
        []
Ralf Seidler committed
72 73 74
      end
    end

75
    def notes
76
      Note.where(project_id: limit_project_ids).user.search(query).order('updated_at DESC')
77 78
    end

79
    def commits
80 81 82 83 84
      if project.empty_repo? || query.blank?
        []
      else
        project.repository.find_commits_by_message(query).compact
      end
85 86
    end

87 88 89 90 91
    def limit_project_ids
      [project.id]
    end
  end
end