2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-23 19:28:12 -04:00
|
|
|
module IssuableCollections
|
|
|
|
extend ActiveSupport::Concern
|
2019-09-03 13:45:00 -04:00
|
|
|
include PaginatedCollection
|
2016-07-23 19:28:12 -04:00
|
|
|
include SortingHelper
|
2019-08-21 06:13:45 -04:00
|
|
|
include SortingPreference
|
2017-11-17 07:27:16 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2016-07-23 19:28:12 -04:00
|
|
|
|
|
|
|
included do
|
2017-11-07 08:34:12 -05:00
|
|
|
helper_method :finder
|
2016-07-23 19:28:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-11-07 08:34:12 -05:00
|
|
|
def set_issuables_index
|
2018-01-29 10:22:58 -05:00
|
|
|
@issuables = issuables_collection
|
2017-09-05 05:22:37 -04:00
|
|
|
|
2019-09-03 13:45:00 -04:00
|
|
|
unless pagination_disabled?
|
|
|
|
set_pagination
|
|
|
|
|
|
|
|
return if redirect_out_of_range(@issuables, @total_pages)
|
|
|
|
end
|
2017-09-05 05:22:37 -04:00
|
|
|
|
2018-02-28 16:58:36 -05:00
|
|
|
if params[:label_name].present? && @project
|
2017-11-07 08:34:12 -05:00
|
|
|
labels_params = { project_id: @project.id, title: params[:label_name] }
|
|
|
|
@labels = LabelsFinder.new(current_user, labels_params).execute
|
2017-09-05 05:22:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@users = []
|
2017-11-07 08:34:12 -05:00
|
|
|
if params[:assignee_id].present?
|
|
|
|
assignee = User.find_by_id(params[:assignee_id])
|
|
|
|
@users.push(assignee) if assignee
|
|
|
|
end
|
2016-07-23 19:28:12 -04:00
|
|
|
|
2017-11-07 08:34:12 -05:00
|
|
|
if params[:author_id].present?
|
|
|
|
author = User.find_by_id(params[:author_id])
|
|
|
|
@users.push(author) if author
|
|
|
|
end
|
2016-07-23 19:28:12 -04:00
|
|
|
end
|
2018-01-29 10:22:58 -05:00
|
|
|
|
|
|
|
def set_pagination
|
2020-10-14 14:08:47 -04:00
|
|
|
row_count = finder.row_count
|
|
|
|
|
2018-01-29 10:22:58 -05:00
|
|
|
@issuables = @issuables.page(params[:page])
|
2019-05-21 18:46:12 -04:00
|
|
|
@issuables = per_page_for_relative_position if params[:sort] == 'relative_position'
|
2020-10-14 14:08:47 -04:00
|
|
|
@issuables = @issuables.without_count if row_count == -1
|
2020-05-27 11:08:11 -04:00
|
|
|
@issuable_meta_data = Gitlab::IssuableMetadata.new(current_user, @issuables).data
|
2020-10-14 14:08:47 -04:00
|
|
|
@total_pages = page_count_for_relation(@issuables, row_count)
|
2018-01-29 10:22:58 -05:00
|
|
|
end
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2016-07-23 19:28:12 -04:00
|
|
|
|
2018-01-29 10:22:58 -05:00
|
|
|
def pagination_disabled?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-11-07 08:34:12 -05:00
|
|
|
def issuables_collection
|
|
|
|
finder.execute.preload(preload_for_collection)
|
2016-07-23 19:28:12 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-07-23 19:28:12 -04:00
|
|
|
|
2017-08-24 12:17:04 -04:00
|
|
|
def page_count_for_relation(relation, row_count)
|
|
|
|
limit = relation.limit_value.to_f
|
|
|
|
|
2020-08-12 02:09:53 -04:00
|
|
|
return 1 if limit == 0
|
2020-10-14 14:08:47 -04:00
|
|
|
return (params[:page] || 1).to_i + 1 if row_count == -1
|
2017-08-24 12:17:04 -04:00
|
|
|
|
|
|
|
(row_count.to_f / limit).ceil
|
|
|
|
end
|
|
|
|
|
2019-05-21 18:46:12 -04:00
|
|
|
# manual / relative_position sorting allows for 100 items on the page
|
|
|
|
def per_page_for_relative_position
|
|
|
|
@issuables.per(100) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
end
|
|
|
|
|
2016-07-23 19:28:12 -04:00
|
|
|
def issuable_finder_for(finder_class)
|
2018-11-09 09:29:45 -05:00
|
|
|
finder_class.new(current_user, finder_options)
|
2016-07-23 19:28:12 -04:00
|
|
|
end
|
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2018-11-09 09:29:45 -05:00
|
|
|
def finder_options
|
2020-07-13 14:09:16 -04:00
|
|
|
strong_memoize(:finder_options) do
|
|
|
|
params[:state] = default_state if params[:state].blank?
|
|
|
|
|
|
|
|
options = {
|
|
|
|
scope: params[:scope],
|
|
|
|
state: params[:state],
|
|
|
|
confidential: Gitlab::Utils.to_boolean(params[:confidential]),
|
|
|
|
sort: set_sort_order
|
|
|
|
}
|
|
|
|
|
|
|
|
# Used by view to highlight active option
|
|
|
|
@sort = options[:sort]
|
|
|
|
|
|
|
|
# When a user looks for an exact iid, we do not filter by search but only by iid
|
|
|
|
if params[:search] =~ /^#(?<iid>\d+)\z/
|
|
|
|
options[:iids] = Regexp.last_match[:iid]
|
|
|
|
params[:search] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if @project
|
|
|
|
options[:project_id] = @project.id
|
|
|
|
options[:attempt_project_search_optimizations] = true
|
|
|
|
elsif @group
|
|
|
|
options[:group_id] = @group.id
|
|
|
|
options[:include_subgroups] = true
|
|
|
|
options[:attempt_group_search_optimizations] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
params.permit(finder_type.valid_params).merge(options)
|
2019-06-18 03:45:47 -04:00
|
|
|
end
|
2016-07-23 19:28:12 -04:00
|
|
|
end
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2016-07-23 19:28:12 -04:00
|
|
|
|
2018-11-09 09:29:45 -05:00
|
|
|
def default_state
|
|
|
|
'opened'
|
2016-07-23 19:28:12 -04:00
|
|
|
end
|
|
|
|
|
2019-08-21 06:13:45 -04:00
|
|
|
def legacy_sort_cookie_name
|
|
|
|
'issuable_sort'
|
2016-07-23 19:28:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_sort_order
|
|
|
|
case params[:state]
|
2017-09-22 20:46:53 -04:00
|
|
|
when 'opened', 'all' then sort_value_created_date
|
2016-07-23 19:28:12 -04:00
|
|
|
when 'merged', 'closed' then sort_value_recently_updated
|
2017-09-22 20:46:53 -04:00
|
|
|
else sort_value_created_date
|
|
|
|
end
|
|
|
|
end
|
2017-11-07 08:34:12 -05:00
|
|
|
|
|
|
|
def finder
|
2018-08-21 05:15:58 -04:00
|
|
|
@finder ||= issuable_finder_for(finder_type)
|
2017-11-07 08:34:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def collection_type
|
2019-07-30 06:04:10 -04:00
|
|
|
@collection_type ||= if finder_type <= IssuesFinder
|
2017-11-07 08:34:12 -05:00
|
|
|
'Issue'
|
2019-07-30 06:04:10 -04:00
|
|
|
elsif finder_type <= MergeRequestsFinder
|
2017-11-07 08:34:12 -05:00
|
|
|
'MergeRequest'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-07 14:35:16 -04:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-11-07 08:34:12 -05:00
|
|
|
def preload_for_collection
|
2019-04-07 14:35:16 -04:00
|
|
|
common_attributes = [:author, :assignees, :labels, :milestone]
|
2017-11-07 08:34:12 -05:00
|
|
|
@preload_for_collection ||= case collection_type
|
|
|
|
when 'Issue'
|
2019-04-07 14:35:16 -04:00
|
|
|
common_attributes + [:project, project: :namespace]
|
2017-11-07 08:34:12 -05:00
|
|
|
when 'MergeRequest'
|
2020-07-17 02:09:11 -04:00
|
|
|
common_attributes + [
|
2020-11-23 13:09:14 -05:00
|
|
|
:target_project, :latest_merge_request_diff, :approvals, :approved_by_users, :reviewers,
|
2020-07-17 02:09:11 -04:00
|
|
|
source_project: :route, head_pipeline: :project, target_project: :namespace
|
|
|
|
]
|
2017-11-07 08:34:12 -05:00
|
|
|
end
|
|
|
|
end
|
2019-04-07 14:35:16 -04:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2016-07-23 19:28:12 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
IssuableCollections.prepend_mod_with('IssuableCollections')
|