BigW Consortium Gitlab

snippet_search_results.rb 955 Bytes
Newer Older
1 2
module Gitlab
  class SnippetSearchResults < SearchResults
3 4
    include SnippetsHelper

5
    attr_reader :limit_snippets
6

7 8
    def initialize(limit_snippets, query)
      @limit_snippets = limit_snippets
9 10 11 12 13 14
      @query = query
    end

    def objects(scope, page = nil)
      case scope
      when 'snippet_titles'
15
        snippet_titles.page(page).per(per_page)
16
      when 'snippet_blobs'
Valery Sizov committed
17
        snippet_blobs.page(page).per(per_page)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
      else
        super
      end
    end

    def snippet_titles_count
      @snippet_titles_count ||= snippet_titles.count
    end

    def snippet_blobs_count
      @snippet_blobs_count ||= snippet_blobs.count
    end

    private

    def snippet_titles
34
      limit_snippets.search(query).order('updated_at DESC').includes(:author)
35 36 37
    end

    def snippet_blobs
38
      limit_snippets.search_code(query).order('updated_at DESC').includes(:author)
39 40 41 42 43 44 45
    end

    def default_scope
      'snippet_blobs'
    end
  end
end