BigW Consortium Gitlab

search_helper.rb 5.08 KB
Newer Older
1
module SearchHelper
2

3
  def search_autocomplete_opts(term)
4
    return unless current_user
5 6 7

    resources_results = [
      groups_autocomplete(term),
8
      projects_autocomplete(term)
9 10 11 12 13
    ].flatten

    generic_results = project_autocomplete + default_autocomplete + help_autocomplete
    generic_results.select! { |result| result[:label] =~ Regexp.new(term, "i") }

14
    [
15 16
      resources_results,
      generic_results
17 18
    ].flatten.uniq do |item|
      item[:label]
19
    end
20 21
  end

Phil Hughes committed
22
  def search_entries_info(collection, scope, term)
23
    return unless collection.count > 0
24

25 26 27 28 29
    from = collection.offset_value + 1
    to = collection.offset_value + collection.length
    count = collection.total_count

    "Showing #{from} - #{to} of #{count} #{scope.humanize(capitalize: false)} for \"#{term}\""
30 31
  end

32 33 34 35 36
  private

  # Autocomplete results for various settings pages
  def default_autocomplete
    [
37 38 39 40
      { category: "Settings", label: "Profile settings", url: profile_path },
      { category: "Settings", label: "SSH Keys",         url: profile_keys_path },
      { category: "Settings", label: "Dashboard",        url: root_path },
      { category: "Settings", label: "Admin Section",    url: admin_root_path },
41 42 43 44 45 46
    ]
  end

  # Autocomplete results for internal help pages
  def help_autocomplete
    [
47 48 49 50 51 52 53 54 55
      { category: "Help", label: "API Help",           url: help_page_path("api", "README") },
      { category: "Help", label: "Markdown Help",      url: help_page_path("markdown", "markdown") },
      { category: "Help", label: "Permissions Help",   url: help_page_path("permissions", "permissions") },
      { category: "Help", label: "Public Access Help", url: help_page_path("public_access", "public_access") },
      { category: "Help", label: "Rake Tasks Help",    url: help_page_path("raketasks", "README") },
      { category: "Help", label: "SSH Keys Help",      url: help_page_path("ssh", "README") },
      { category: "Help", label: "System Hooks Help",  url: help_page_path("system_hooks", "system_hooks") },
      { category: "Help", label: "Webhooks Help",      url: help_page_path("web_hooks", "web_hooks") },
      { category: "Help", label: "Workflow Help",      url: help_page_path("workflow", "README") },
56 57 58 59 60 61 62 63 64
    ]
  end

  # Autocomplete results for the current project, if it's defined
  def project_autocomplete
    if @project && @project.repository.exists? && @project.repository.root_ref
      ref    = @ref || @project.repository.root_ref

      [
65 66 67 68 69 70 71 72 73 74
        { category: "Current Project", label: "Files",          url: namespace_project_tree_path(@project.namespace, @project, ref) },
        { category: "Current Project", label: "Commits",        url: namespace_project_commits_path(@project.namespace, @project, ref) },
        { category: "Current Project", label: "Network",        url: namespace_project_network_path(@project.namespace, @project, ref) },
        { category: "Current Project", label: "Graph",          url: namespace_project_graph_path(@project.namespace, @project, ref) },
        { category: "Current Project", label: "Issues",         url: namespace_project_issues_path(@project.namespace, @project) },
        { category: "Current Project", label: "Merge Requests", url: namespace_project_merge_requests_path(@project.namespace, @project) },
        { category: "Current Project", label: "Milestones",     url: namespace_project_milestones_path(@project.namespace, @project) },
        { category: "Current Project", label: "Snippets",       url: namespace_project_snippets_path(@project.namespace, @project) },
        { category: "Current Project", label: "Members",        url: namespace_project_project_members_path(@project.namespace, @project) },
        { category: "Current Project", label: "Wiki",           url: namespace_project_wikis_path(@project.namespace, @project) },
75 76 77 78 79 80 81
      ]
    else
      []
    end
  end

  # Autocomplete results for the current user's groups
82
  def groups_autocomplete(term, limit = 5)
83
    current_user.authorized_groups.search(term).limit(limit).map do |group|
84
      {
85
        category: "Groups",
86 87
        id: group.id,
        label: "#{search_result_sanitize(group.name)}",
88 89
        url: group_path(group)
      }
90 91 92 93
    end
  end

  # Autocomplete results for the current user's projects
94
  def projects_autocomplete(term, limit = 5)
95
    current_user.authorized_projects.search_by_title(term).
96
      sorted_by_stars.non_archived.limit(limit).map do |p|
97
      {
98
        category: "Projects",
99 100 101
        id: p.id,
        value: "#{search_result_sanitize(p.name)}",
        label: "#{search_result_sanitize(p.name_with_namespace)}",
Vinnie Okada committed
102
        url: namespace_project_path(p.namespace, p)
103
      }
104 105
    end
  end
106

107
  def search_result_sanitize(str)
108 109
    Sanitize.clean(str)
  end
110 111 112 113 114 115 116 117 118 119

  def search_filter_path(options={})
    exist_opts = {
      search: params[:search],
      project_id: params[:project_id],
      group_id: params[:group_id],
      scope: params[:scope]
    }

    options = exist_opts.merge(options)
120
    search_path(options)
121
  end
122 123 124

  # Sanitize html generated after parsing markdown from issue description or comment
  def search_md_sanitize(html)
125
    sanitize(html, tags: %w(a p ol ul li pre code))
126
  end
127
end