diff options
Diffstat (limited to 'lib/gitlab/search_results.rb')
-rw-r--r-- | lib/gitlab/search_results.rb | 67 |
1 files changed, 48 insertions, 19 deletions
diff --git a/lib/gitlab/search_results.rb b/lib/gitlab/search_results.rb index 06d8dca2f70..b81264c5d0c 100644 --- a/lib/gitlab/search_results.rb +++ b/lib/gitlab/search_results.rb @@ -7,7 +7,7 @@ module Gitlab DEFAULT_PAGE = 1 DEFAULT_PER_PAGE = 20 - attr_reader :current_user, :query, :filters + attr_reader :current_user, :query, :sort, :filters # Limit search results by passed projects # It allows us to search only for projects user has access to @@ -19,31 +19,23 @@ module Gitlab # query attr_reader :default_project_filter - def initialize(current_user, query, limit_projects = nil, default_project_filter: false, filters: {}) + def initialize(current_user, query, limit_projects = nil, sort: nil, default_project_filter: false, filters: {}) @current_user = current_user @query = query @limit_projects = limit_projects || Project.all @default_project_filter = default_project_filter + @sort = sort @filters = filters end def objects(scope, page: nil, per_page: DEFAULT_PER_PAGE, without_count: true, preload_method: nil) should_preload = preload_method.present? - collection = case scope - when 'projects' - projects - when 'issues' - issues - when 'merge_requests' - merge_requests - when 'milestones' - milestones - when 'users' - users - else - should_preload = false - Kaminari.paginate_array([]) - end + collection = collection_for(scope) + + if collection.nil? + should_preload = false + collection = Kaminari.paginate_array([]) + end collection = collection.public_send(preload_method) if should_preload # rubocop:disable GitlabSecurity/PublicSend collection = collection.page(page).per(per_page) @@ -116,8 +108,41 @@ module Gitlab UsersFinder.new(current_user, search: query).execute end + # highlighting is only performed by Elasticsearch backed results + def highlight_map(scope) + {} + end + private + def collection_for(scope) + case scope + when 'projects' + projects + when 'issues' + issues + when 'merge_requests' + merge_requests + when 'milestones' + milestones + when 'users' + users + end + end + + # rubocop: disable CodeReuse/ActiveRecord + def apply_sort(scope) + case sort + when 'oldest' + scope.reorder('created_at ASC') + when 'newest' + scope.reorder('created_at DESC') + else + scope + end + end + # rubocop: enable CodeReuse/ActiveRecord + def projects limit_projects.search(query) end @@ -129,7 +154,7 @@ module Gitlab issues = issues.where(project_id: project_ids_relation) # rubocop: disable CodeReuse/ActiveRecord end - issues + apply_sort(issues) end # rubocop: disable CodeReuse/ActiveRecord @@ -149,7 +174,7 @@ module Gitlab merge_requests = merge_requests.in_projects(project_ids_relation) end - merge_requests + apply_sort(merge_requests) end def default_scope @@ -193,6 +218,10 @@ module Gitlab end params[:state] = filters[:state] if filters.key?(:state) + + if [true, false].include?(filters[:confidential]) && Feature.enabled?(:search_filter_by_confidential) + params[:confidential] = filters[:confidential] + end end end |