debian-mirror-gitlab/app/controllers/concerns/issuable_collections.rb

216 lines
6.2 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
module IssuableCollections
extend ActiveSupport::Concern
2018-11-20 20:47:30 +05:30
include CookiesHelper
2016-09-13 17:45:13 +05:30
include SortingHelper
2017-09-10 17:25:29 +05:30
include Gitlab::IssuableMetadata
2018-03-17 18:26:18 +05:30
include Gitlab::Utils::StrongMemoize
2016-09-13 17:45:13 +05:30
included do
2018-03-17 18:26:18 +05:30
helper_method :finder
2016-09-13 17:45:13 +05:30
end
private
2018-03-17 18:26:18 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
def set_issuables_index
@issuables = issuables_collection
set_pagination
return if redirect_out_of_range(@total_pages)
2018-03-27 19:54:05 +05:30
if params[:label_name].present? && @project
2018-03-17 18:26:18 +05:30
labels_params = { project_id: @project.id, title: params[:label_name] }
@labels = LabelsFinder.new(current_user, labels_params).execute
end
@users = []
if params[:assignee_id].present?
assignee = User.find_by_id(params[:assignee_id])
@users.push(assignee) if assignee
end
if params[:author_id].present?
author = User.find_by_id(params[:author_id])
@users.push(author) if author
end
end
def set_pagination
return if pagination_disabled?
@issuables = @issuables.page(params[:page])
2019-09-04 21:01:54 +05:30
@issuables = per_page_for_relative_position if params[:sort] == 'relative_position'
2019-07-07 11:18:12 +05:30
@issuable_meta_data = issuable_meta_data(@issuables, collection_type, current_user)
2018-03-17 18:26:18 +05:30
@total_pages = issuable_page_count
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
def pagination_disabled?
false
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def issuables_collection
finder.execute.preload(preload_for_collection)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def redirect_out_of_range(total_pages)
return false if total_pages.nil? || total_pages.zero?
out_of_range = @issuables.current_page > total_pages # rubocop:disable Gitlab/ModuleWithInstanceVariables
if out_of_range
2018-06-27 16:04:02 +05:30
redirect_to(url_for(safe_params.merge(page: total_pages, only_path: true)))
2018-03-17 18:26:18 +05:30
end
out_of_range
2016-09-13 17:45:13 +05:30
end
2018-03-17 18:26:18 +05:30
def issuable_page_count
page_count_for_relation(@issuables, finder.row_count) # rubocop:disable Gitlab/ModuleWithInstanceVariables
2016-09-13 17:45:13 +05:30
end
2018-03-17 18:26:18 +05:30
def page_count_for_relation(relation, row_count)
limit = relation.limit_value.to_f
return 1 if limit.zero?
(row_count.to_f / limit).ceil
2016-09-13 17:45:13 +05:30
end
2019-09-04 21:01:54 +05:30
# 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-09-13 17:45:13 +05:30
def issuable_finder_for(finder_class)
2019-02-15 15:39:39 +05:30
finder_class.new(current_user, finder_options)
2016-09-13 17:45:13 +05:30
end
2018-03-17 18:26:18 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2019-02-15 15:39:39 +05:30
def finder_options
params[:state] = default_state if params[:state].blank?
2016-09-13 17:45:13 +05:30
2019-02-15 15:39:39 +05:30
options = {
scope: params[:scope],
state: params[:state],
2019-07-07 11:18:12 +05:30
confidential: Gitlab::Utils.to_boolean(params[:confidential]),
2019-02-15 15:39:39 +05:30
sort: set_sort_order
}
2016-09-13 17:45:13 +05:30
2019-02-15 15:39:39 +05:30
# Used by view to highlight active option
@sort = options[:sort]
2016-09-13 17:45:13 +05:30
2019-09-30 21:07:59 +05:30
# 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
2016-09-13 17:45:13 +05:30
if @project
2019-02-15 15:39:39 +05:30
options[:project_id] = @project.id
2019-07-07 11:18:12 +05:30
options[:attempt_project_search_optimizations] = true
2016-09-13 17:45:13 +05:30
elsif @group
2019-02-15 15:39:39 +05:30
options[:group_id] = @group.id
options[:include_subgroups] = true
options[:attempt_group_search_optimizations] = true
2016-09-13 17:45:13 +05:30
end
2019-02-15 15:39:39 +05:30
params.permit(finder_type.valid_params).merge(options)
2016-09-13 17:45:13 +05:30
end
2018-03-17 18:26:18 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2016-09-13 17:45:13 +05:30
2019-02-15 15:39:39 +05:30
def default_state
'opened'
end
def set_sort_order
set_sort_order_from_user_preference || set_sort_order_from_cookie || default_sort_order
end
def set_sort_order_from_user_preference
return unless current_user
return unless issuable_sorting_field
user_preference = current_user.user_preference
sort_param = params[:sort]
sort_param ||= user_preference[issuable_sorting_field]
return sort_param if Gitlab::Database.read_only?
if user_preference[issuable_sorting_field] != sort_param
2019-03-02 22:35:43 +05:30
user_preference.update(issuable_sorting_field => sort_param)
2019-02-15 15:39:39 +05:30
end
sort_param
end
2019-03-02 22:35:43 +05:30
# Implement issuable_sorting_field method on controllers
2019-02-15 15:39:39 +05:30
# to choose which column to store the sorting parameter.
def issuable_sorting_field
nil
2016-09-13 17:45:13 +05:30
end
def set_sort_order_from_cookie
2018-11-20 20:47:30 +05:30
sort_param = params[:sort] if params[:sort].present?
# fallback to legacy cookie value for backward compatibility
sort_param ||= cookies['issuable_sort']
sort_param ||= cookies[remember_sorting_key]
2016-09-13 17:45:13 +05:30
2018-11-20 20:47:30 +05:30
sort_value = update_cookie_value(sort_param)
set_secure_cookie(remember_sorting_key, sort_value)
2019-02-15 15:39:39 +05:30
sort_value
2018-11-20 20:47:30 +05:30
end
def remember_sorting_key
@remember_sorting_key ||= "#{collection_type.downcase}_sort"
2016-09-13 17:45:13 +05:30
end
def default_sort_order
case params[:state]
2018-03-17 18:26:18 +05:30
when 'opened', 'all' then sort_value_created_date
2016-09-13 17:45:13 +05:30
when 'merged', 'closed' then sort_value_recently_updated
2018-03-17 18:26:18 +05:30
else sort_value_created_date
end
end
# Update old values to the actual ones.
def update_cookie_value(value)
case value
when 'id_asc' then sort_value_oldest_created
when 'id_desc' then sort_value_recently_created
when 'downvotes_asc' then sort_value_popularity
when 'downvotes_desc' then sort_value_popularity
else value
end
end
def finder
2018-11-20 20:47:30 +05:30
@finder ||= issuable_finder_for(finder_type)
2016-09-13 17:45:13 +05:30
end
2018-03-17 18:26:18 +05:30
def collection_type
2019-10-12 21:52:04 +05:30
@collection_type ||= if finder_type <= IssuesFinder
2018-03-17 18:26:18 +05:30
'Issue'
2019-10-12 21:52:04 +05:30
elsif finder_type <= MergeRequestsFinder
2018-03-17 18:26:18 +05:30
'MergeRequest'
end
end
2019-07-31 22:56:46 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2018-03-17 18:26:18 +05:30
def preload_for_collection
2019-07-31 22:56:46 +05:30
common_attributes = [:author, :assignees, :labels, :milestone]
2018-03-17 18:26:18 +05:30
@preload_for_collection ||= case collection_type
when 'Issue'
2019-07-31 22:56:46 +05:30
common_attributes + [:project, project: :namespace]
2018-03-17 18:26:18 +05:30
when 'MergeRequest'
2019-07-31 22:56:46 +05:30
common_attributes + [:target_project, source_project: :route, head_pipeline: :project, target_project: :namespace, latest_merge_request_diff: :merge_request_diff_commits]
2018-03-17 18:26:18 +05:30
end
end
2019-07-31 22:56:46 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2016-09-13 17:45:13 +05:30
end