BigW Consortium Gitlab

search_helper.rb 5.05 KB
Newer Older
1
module SearchHelper
2
  def search_autocomplete_opts(term)
3
    return unless current_user
4 5 6

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

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

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

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

24 25 26 27 28
    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}\""
29 30
  end

31 32 33 34 35
  private

  # Autocomplete results for various settings pages
  def default_autocomplete
    [
36 37 38 39
      { 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 },
40 41 42 43 44 45
    ]
  end

  # Autocomplete results for internal help pages
  def help_autocomplete
    [
46 47
      { category: "Help", label: "API Help",           url: help_page_path("api/README") },
      { category: "Help", label: "Markdown Help",      url: help_page_path("markdown/markdown") },
48
      { category: "Help", label: "Permissions Help",   url: help_page_path("user/permissions") },
49 50 51 52 53 54
      { 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") },
55 56 57 58 59 60
    ]
  end

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

      [
64 65 66 67 68 69 70 71 72 73
        { 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) },
74 75 76 77 78 79 80
      ]
    else
      []
    end
  end

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

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

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

  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)
119
    search_path(options)
120
  end
121 122 123

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