BigW Consortium Gitlab

search_results.rb 2.16 KB
Newer Older
1 2
module Gitlab
  class SearchResults
3
    attr_reader :query
4 5 6 7 8

    # Limit search results by passed project ids
    # It allows us to search only for projects user has access to
    attr_reader :limit_project_ids

9
    def initialize(limit_project_ids, query)
10 11 12 13
      @limit_project_ids = limit_project_ids || Project.all
      @query = Shellwords.shellescape(query) if query.present?
    end

Dmitriy Zaporozhets committed
14
    def objects(scope, page = nil)
15 16 17 18 19 20 21
      case scope
      when 'projects'
        projects.page(page).per(per_page)
      when 'issues'
        issues.page(page).per(per_page)
      when 'merge_requests'
        merge_requests.page(page).per(per_page)
22 23
      when 'milestones'
        milestones.page(page).per(per_page)
24 25 26 27 28 29
      else
        Kaminari.paginate_array([]).page(page).per(per_page)
      end
    end

    def total_count
30
      @total_count ||= projects_count + issues_count + merge_requests_count + milestones_count
31 32 33 34 35 36 37 38 39 40 41 42 43 44
    end

    def projects_count
      @projects_count ||= projects.count
    end

    def issues_count
      @issues_count ||= issues.count
    end

    def merge_requests_count
      @merge_requests_count ||= merge_requests.count
    end

45 46 47 48
    def milestones_count
      @milestones_count ||= milestones.count
    end

49 50 51 52 53 54 55 56 57 58 59
    def empty?
      total_count.zero?
    end

    private

    def projects
      Project.where(id: limit_project_ids).search(query)
    end

    def issues
60 61 62 63 64 65 66
      issues = Issue.where(project_id: limit_project_ids)
      if query =~ /#(\d+)\z/
        issues = issues.where(iid: $1)
      else
        issues = issues.full_search(query)
      end
      issues.order('updated_at DESC')
67 68
    end

69 70 71 72 73 74
    def milestones
      milestones = Milestone.where(project_id: limit_project_ids)
      milestones = milestones.search(query)
      milestones.order('updated_at DESC')
    end

75
    def merge_requests
76 77 78 79 80 81 82
      merge_requests = MergeRequest.in_projects(limit_project_ids)
      if query =~ /[#!](\d+)\z/
        merge_requests = merge_requests.where(iid: $1)
      else
        merge_requests = merge_requests.full_search(query)
      end
      merge_requests.order('updated_at DESC')
83 84 85 86 87 88 89 90 91 92 93
    end

    def default_scope
      'projects'
    end

    def per_page
      20
    end
  end
end