diff options
author | Mario de la Ossa <mariodelaossa@gmail.com> | 2018-06-06 21:54:09 -0600 |
---|---|---|
committer | Mario de la Ossa <mariodelaossa@gmail.com> | 2018-06-06 23:23:34 -0600 |
commit | f132c644bb772a3f69cf552b4dc1da6f4b13b3a0 (patch) | |
tree | c210d227f4e90b02bec06cd356a33e24883d5aa6 | |
parent | 60b102be3d0d40cd36141cfcea0aecff2f115a06 (diff) | |
download | gitlab-ce-ce-4745-epic-search.tar.gz |
Backport of "Add Epics to global search"ce-4745-epic-search
-rw-r--r-- | app/helpers/application_helper.rb | 12 | ||||
-rw-r--r-- | app/helpers/search_helper.rb | 87 | ||||
-rw-r--r-- | app/services/search/global_service.rb | 8 | ||||
-rw-r--r-- | app/views/search/_category.html.haml | 80 | ||||
-rw-r--r-- | app/views/search/_results.html.haml | 2 | ||||
-rw-r--r-- | lib/gitlab/search_results.rb | 29 |
6 files changed, 125 insertions, 93 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f5d94ad96a1..8c51ba65374 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3,8 +3,16 @@ require 'uri' module ApplicationHelper # See https://docs.gitlab.com/ee/development/ee_features.html#code-in-app-views - def render_if_exists(partial, locals = {}) - render(partial, locals) if lookup_context.exists?(partial, [], true) + # We allow partial to be nil so that collection views can be passed in + # `render partial: 'some/view', collection: @some_collection` + def render_if_exists(partial = nil, **options) + return unless lookup_context.exists?(partial || options[:partial], [], true) + + if partial.nil? + render(**options) + else + render(partial, options) + end end # Check if a particular controller is the current one diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index f7dafca7834..1fd797259d9 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -179,6 +179,93 @@ module SearchHelper sanitize(html, tags: %w(a p ol ul li pre code)) end + def project_category_names + %i[blobs issues merge_requests milestones notes wiki_blobs commits].freeze + end + + def snippet_categories + { + snippet_blobs: { + title: 'Snippet Contents', + count: -> { @search_results.snippet_blobs_count }, + link: search_filter_path(scope: 'snippet_blobs', snippets: true, group_id: nil, project_id: nil) + }, + snippet_titles: { + title: 'Titles and Filenames', + count: -> { @search_results.snippet_titles_count }, + link: search_filter_path(scope: 'snippet_titles', snippets: true, group_id: nil, project_id: nil) + } + } + end + + def categories + { + projects: { + title: 'Projects', + count: -> { limited_count(@search_results.limited_projects_count) }, + link: search_filter_path(scope: 'projects') + }, + issues: { + title: 'Issues', + count: -> { limited_count(@search_results.limited_issues_count) }, + link: search_filter_path(scope: 'issues') + }, + merge_requests: { + title: 'Merge requests', + count: -> { limited_count(@search_results.limited_merge_requests_count) }, + link: search_filter_path(scope: 'merge_requests') + }, + milestones: { + title: 'Milestones', + count: -> { limited_count(@search_results.limited_milestones_count) }, + link: search_filter_path(scope: 'milestones') + }, + notes: { + title: 'Comments', + count: -> { limited_count(@search_results.limited_notes_count) }, + link: search_filter_path(scope: 'notes') + }, + blobs: { + title: 'Code', + count: -> { limited_count(@search_results.blobs_count) }, + link: search_filter_path(scope: 'blobs') + }, + commits: { + title: 'Commits', + count: -> { limited_count(@search_results.commits_count) }, + link: search_filter_path(scope: 'commits') + }, + wiki_blobs: { + title: 'Wiki', + count: -> { limited_count(@search_results.wiki_blobs_count) }, + link: search_filter_path(scope: 'wiki_blobs') + } + } + end + + def skipped_global_categories + %i[blobs commits wiki_blobs].freeze + end + + def category_tabs + if @project + # Doing it this way to keep the order of the keys + cats = project_category_names.each_with_object({}) { |cat_name, hash| hash[cat_name] = categories[cat_name] } + + cats.select do |key, _v| + key = :wiki if key == :wiki_blobs + project_search_tabs?(key) + end + elsif @show_snippets + snippet_categories + else + categories.tap do |hash| + hash.delete(:notes) + hash.except!(*skipped_global_categories) + end + end + end + def limited_count(count, limit = 1000) count > limit ? "#{limit}+" : count end diff --git a/app/services/search/global_service.rb b/app/services/search/global_service.rb index 92a32e703af..c415b8b42a0 100644 --- a/app/services/search/global_service.rb +++ b/app/services/search/global_service.rb @@ -19,10 +19,14 @@ module Search def scope @scope ||= begin - allowed_scopes = %w[issues merge_requests milestones] - allowed_scopes.delete(params[:scope]) { 'projects' } end end + + private + + def allowed_scopes + %w[issues merge_requests milestones] + end end end diff --git a/app/views/search/_category.html.haml b/app/views/search/_category.html.haml index ff9a7b53a86..6dda27ffb13 100644 --- a/app/views/search/_category.html.haml +++ b/app/views/search/_category.html.haml @@ -2,79 +2,9 @@ .fade-left= icon('angle-left') .fade-right= icon('angle-right') %ul.nav-links.search-filter.scrolling-tabs.nav.nav-tabs - - if @project - - if project_search_tabs?(:blobs) - %li{ class: active_when(@scope == 'blobs') } - = link_to search_filter_path(scope: 'blobs') do - Code - %span.badge.badge-pill - = @search_results.blobs_count - - if project_search_tabs?(:issues) - %li{ class: active_when(@scope == 'issues') } - = link_to search_filter_path(scope: 'issues') do - Issues - %span.badge.badge-pill - = limited_count(@search_results.limited_issues_count) - - if project_search_tabs?(:merge_requests) - %li{ class: active_when(@scope == 'merge_requests') } - = link_to search_filter_path(scope: 'merge_requests') do - Merge requests - %span.badge.badge-pill - = limited_count(@search_results.limited_merge_requests_count) - - if project_search_tabs?(:milestones) - %li{ class: active_when(@scope == 'milestones') } - = link_to search_filter_path(scope: 'milestones') do - Milestones - %span.badge.badge-pill - = limited_count(@search_results.limited_milestones_count) - - if project_search_tabs?(:notes) - %li{ class: active_when(@scope == 'notes') } - = link_to search_filter_path(scope: 'notes') do - Comments - %span.badge.badge-pill - = limited_count(@search_results.limited_notes_count) - - if project_search_tabs?(:wiki) - %li{ class: active_when(@scope == 'wiki_blobs') } - = link_to search_filter_path(scope: 'wiki_blobs') do - Wiki - %span.badge.badge-pill - = @search_results.wiki_blobs_count - - if project_search_tabs?(:commits) - %li{ class: active_when(@scope == 'commits') } - = link_to search_filter_path(scope: 'commits') do - Commits - %span.badge.badge-pill - = @search_results.commits_count - - - elsif @show_snippets - %li{ class: active_when(@scope == 'snippet_blobs') } - = link_to search_filter_path(scope: 'snippet_blobs', snippets: true, group_id: nil, project_id: nil) do - Snippet Contents + - category_tabs&.each do |category, cat_attrs| + %li{ class: active_when(@scope == category.to_s) } + = link_to cat_attrs[:link] do + = cat_attrs[:title] %span.badge.badge-pill - = @search_results.snippet_blobs_count - %li{ class: active_when(@scope == 'snippet_titles') } - = link_to search_filter_path(scope: 'snippet_titles', snippets: true, group_id: nil, project_id: nil) do - Titles and Filenames - %span.badge.badge-pill - = @search_results.snippet_titles_count - - else - %li{ class: active_when(@scope == 'projects') } - = link_to search_filter_path(scope: 'projects') do - Projects - %span.badge.badge-pill - = limited_count(@search_results.limited_projects_count) - %li{ class: active_when(@scope == 'issues') } - = link_to search_filter_path(scope: 'issues') do - Issues - %span.badge.badge-pill - = limited_count(@search_results.limited_issues_count) - %li{ class: active_when(@scope == 'merge_requests') } - = link_to search_filter_path(scope: 'merge_requests') do - Merge requests - %span.badge.badge-pill - = limited_count(@search_results.limited_merge_requests_count) - %li{ class: active_when(@scope == 'milestones') } - = link_to search_filter_path(scope: 'milestones') do - Milestones - %span.badge.badge-pill - = limited_count(@search_results.limited_milestones_count) + = cat_attrs[:count].call() diff --git a/app/views/search/_results.html.haml b/app/views/search/_results.html.haml index ab56f48ba4d..58433776148 100644 --- a/app/views/search/_results.html.haml +++ b/app/views/search/_results.html.haml @@ -20,7 +20,7 @@ .term = render 'shared/projects/list', projects: @search_objects - else - = render partial: "search/results/#{@scope.singularize}", collection: @search_objects + = render_if_exists partial: "search/results/#{@scope.singularize}", collection: @search_objects - if @scope != 'projects' = paginate_collection(@search_objects) diff --git a/lib/gitlab/search_results.rb b/lib/gitlab/search_results.rb index 1e45d074e0a..952b861d779 100644 --- a/lib/gitlab/search_results.rb +++ b/lib/gitlab/search_results.rb @@ -46,19 +46,7 @@ module Gitlab end def objects(scope, page = nil, without_count = true) - collection = 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) - when 'milestones' - milestones.page(page).per(per_page) - else - Kaminari.paginate_array([]).page(page).per(per_page) - end - + collection = collection(scope, page) without_count ? collection.without_count : collection end @@ -96,6 +84,21 @@ module Gitlab private + def collection(scope, page) + 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) + when 'milestones' + milestones.page(page).per(per_page) + else + Kaminari.paginate_array([]).page(page).per(per_page) + end + end + def projects limit_projects.search(query) end |