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

174 lines
5.1 KiB
Ruby
Raw Normal View History

2016-09-13 17:45:13 +05:30
module IssuableCollections
extend ActiveSupport::Concern
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])
@issuable_meta_data = issuable_meta_data(@issuables, collection_type)
@total_pages = issuable_page_count
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
def pagination_disabled?
false
2016-09-13 17:45:13 +05:30
end
2018-03-17 18:26:18 +05:30
def issuables_collection
finder.execute.preload(preload_for_collection)
end
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
def issuable_finder_for(finder_class)
finder_class.new(current_user, filter_params)
end
2018-03-17 18:26:18 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2016-09-13 17:45:13 +05:30
def filter_params
set_sort_order_from_cookie
set_default_state
2017-09-10 17:25:29 +05:30
# Skip irrelevant Rails routing params
@filter_params = params.dup.except(:controller, :action, :namespace_id)
2016-09-13 17:45:13 +05:30
@filter_params[:sort] ||= default_sort_order
@sort = @filter_params[:sort]
if @project
@filter_params[:project_id] = @project.id
elsif @group
@filter_params[:group_id] = @group.id
2018-03-17 18:26:18 +05:30
@filter_params[:include_subgroups] = true
2016-09-13 17:45:13 +05:30
else
# TODO: this filter ignore issues/mr created in public or
# internal repos where you are not a member. Enable this filter
# or improve current implementation to filter only issues you
# created or assigned or mentioned
# @filter_params[:authorized_only] = true
end
2018-03-27 19:54:05 +05:30
@filter_params.permit(finder_type.valid_params)
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
def set_default_state
params[:state] = 'opened' if params[:state].blank?
end
def set_sort_order_from_cookie
key = 'issuable_sort'
cookies[key] = params[:sort] if params[:sort].present?
2018-03-17 18:26:18 +05:30
cookies[key] = update_cookie_value(cookies[key])
2016-09-13 17:45:13 +05:30
params[:sort] = cookies[key]
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 'created_asc' then sort_value_created_date
when 'created_desc' then sort_value_created_date
when 'due_date_asc' then sort_value_due_date
when 'due_date_desc' then sort_value_due_date
when 'milestone_due_asc' then sort_value_milestone
when 'milestone_due_desc' then sort_value_milestone
when 'downvotes_asc' then sort_value_popularity
when 'downvotes_desc' then sort_value_popularity
else value
end
end
def finder
strong_memoize(:finder) do
2018-03-27 19:54:05 +05:30
issuable_finder_for(finder_type)
2016-09-13 17:45:13 +05:30
end
end
2018-03-17 18:26:18 +05:30
def collection_type
@collection_type ||= case finder
when IssuesFinder
'Issue'
when MergeRequestsFinder
'MergeRequest'
end
end
def preload_for_collection
@preload_for_collection ||= case collection_type
when 'Issue'
[:project, :author, :assignees, :labels, :milestone, project: :namespace]
when 'MergeRequest'
[
2018-10-15 14:42:47 +05:30
:target_project, :author, :assignee, :labels, :milestone,
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
2016-09-13 17:45:13 +05:30
end