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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
2.7 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
module SortingPreference
include SortingHelper
include CookiesHelper
2021-02-22 17:27:13 +05:30
def set_sort_order(field = sorting_field, default_order = default_sort_order)
2022-07-23 23:45:48 +05:30
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-12-04 20:38:33 +05:30
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
2021-02-22 17:27:13 +05:30
def set_sort_order_from_user_preference(field = sorting_field)
2019-12-04 20:38:33 +05:30
return unless current_user
2021-02-22 17:27:13 +05:30
return unless field
2019-12-04 20:38:33 +05:30
user_preference = current_user.user_preference
sort_param = params[:sort]
2021-02-22 17:27:13 +05:30
sort_param ||= user_preference[field]
2019-12-04 20:38:33 +05:30
return sort_param if Gitlab::Database.read_only?
2021-02-22 17:27:13 +05:30
if user_preference[field] != sort_param
user_preference.update(field => sort_param)
2019-12-04 20:38:33 +05:30
end
sort_param
end
2021-02-22 17:27:13 +05:30
def set_sort_order_from_cookie(field = sorting_field)
2019-12-04 20:38:33 +05:30
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]
2021-02-22 17:27:13 +05:30
sort_param ||= cookies[remember_sorting_key(field)]
2019-12-04 20:38:33 +05:30
sort_value = update_cookie_value(sort_param)
2021-02-22 17:27:13 +05:30
set_secure_cookie(remember_sorting_key(field), sort_value)
2019-12-04 20:38:33 +05:30
sort_value
end
# Convert sorting_field to legacy cookie name for backwards compatibility
# :merge_requests_sort => 'mergerequest_sort'
# :issues_sort => 'issue_sort'
2021-02-22 17:27:13 +05:30
def remember_sorting_key(field = sorting_field)
@remember_sorting_key ||= field
2019-12-04 20:38:33 +05:30
.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-07-23 23:45:48 +05:30
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-12-04 20:38:33 +05:30
end