BigW Consortium Gitlab

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

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

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

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

    def blobs_count
      @blobs_count ||= blobs.count
    end

    def notes_count
      @notes_count ||= notes.count
    end

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

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

52 53 54
    private

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

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

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

76
    def notes
77
      project.notes.user.search(query, as_user: @current_user).order('updated_at DESC')
78 79
    end

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

    def project_ids_relation
      project
    end
91 92
  end
end