2019-08-21 06:13:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module SortingPreference
|
|
|
|
include SortingHelper
|
|
|
|
include CookiesHelper
|
|
|
|
|
2020-11-23 10:09:37 -05:00
|
|
|
def set_sort_order(field = sorting_field, default_order = default_sort_order)
|
2022-06-03 05:08:43 -04:00
|
|
|
sort_order = set_sort_order_from_user_preference(field) || set_sort_order_from_cookie(field) || params[:sort]
|
|
|
|
|
|
|
|
# some types of sorting might not be available on the dashboard
|
|
|
|
return default_order unless valid_sort_order?(sort_order)
|
|
|
|
|
|
|
|
sort_order
|
2019-08-21 06:13:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Implement sorting_field method on controllers
|
|
|
|
# to choose which column to store the sorting parameter.
|
|
|
|
def sorting_field
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Implement default_sort_order method on controllers
|
|
|
|
# to choose which default sort should be applied if
|
|
|
|
# sort param is not provided.
|
|
|
|
def default_sort_order
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Implement legacy_sort_cookie_name method on controllers
|
|
|
|
# to set sort from cookie for backwards compatibility.
|
|
|
|
def legacy_sort_cookie_name
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-11-23 10:09:37 -05:00
|
|
|
def set_sort_order_from_user_preference(field = sorting_field)
|
2019-08-21 06:13:45 -04:00
|
|
|
return unless current_user
|
2020-11-23 10:09:37 -05:00
|
|
|
return unless field
|
2019-08-21 06:13:45 -04:00
|
|
|
|
|
|
|
user_preference = current_user.user_preference
|
|
|
|
|
|
|
|
sort_param = params[:sort]
|
2020-11-23 10:09:37 -05:00
|
|
|
sort_param ||= user_preference[field]
|
2019-08-21 06:13:45 -04:00
|
|
|
|
2021-08-02 05:10:09 -04:00
|
|
|
return sort_param if Gitlab::Database.read_only?
|
2019-08-21 06:13:45 -04:00
|
|
|
|
2020-11-23 10:09:37 -05:00
|
|
|
if user_preference[field] != sort_param
|
|
|
|
user_preference.update(field => sort_param)
|
2019-08-21 06:13:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
sort_param
|
|
|
|
end
|
|
|
|
|
2020-11-23 10:09:37 -05:00
|
|
|
def set_sort_order_from_cookie(field = sorting_field)
|
2019-08-21 06:13:45 -04:00
|
|
|
return unless legacy_sort_cookie_name
|
|
|
|
|
|
|
|
sort_param = params[:sort] if params[:sort].present?
|
|
|
|
# fallback to legacy cookie value for backward compatibility
|
|
|
|
sort_param ||= cookies[legacy_sort_cookie_name]
|
2020-11-23 10:09:37 -05:00
|
|
|
sort_param ||= cookies[remember_sorting_key(field)]
|
2019-08-21 06:13:45 -04:00
|
|
|
|
|
|
|
sort_value = update_cookie_value(sort_param)
|
2020-11-23 10:09:37 -05:00
|
|
|
set_secure_cookie(remember_sorting_key(field), sort_value)
|
2019-08-21 06:13:45 -04:00
|
|
|
sort_value
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert sorting_field to legacy cookie name for backwards compatibility
|
|
|
|
# :merge_requests_sort => 'mergerequest_sort'
|
|
|
|
# :issues_sort => 'issue_sort'
|
2020-11-23 10:09:37 -05:00
|
|
|
def remember_sorting_key(field = sorting_field)
|
|
|
|
@remember_sorting_key ||= field
|
2019-08-21 06:13:45 -04:00
|
|
|
.to_s
|
|
|
|
.split('_')[0..-2]
|
|
|
|
.map(&:singularize)
|
|
|
|
.join('')
|
|
|
|
.concat('_sort')
|
|
|
|
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
|
2022-06-03 05:08:43 -04:00
|
|
|
|
|
|
|
def valid_sort_order?(sort_order)
|
|
|
|
return false unless sort_order
|
|
|
|
return can_sort_by_issue_weight?(action_name == 'issues') if sort_order.include?('weight')
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2019-08-21 06:13:45 -04:00
|
|
|
end
|