Commit 95d2846d authored by Sean McGivern's avatar Sean McGivern

Merge branch '18871_remember_project_ordering' into 'master'

Remember project ordering when inside groups

See merge request gitlab-org/gitlab!47850
parents db6e4b74 c68f6de8
......@@ -4,8 +4,11 @@ module SortingPreference
include SortingHelper
include CookiesHelper
def set_sort_order
set_sort_order_from_user_preference || set_sort_order_from_cookie || params[:sort] || default_sort_order
def set_sort_order(field = sorting_field, default_order = default_sort_order)
set_sort_order_from_user_preference(field) ||
set_sort_order_from_cookie(field) ||
params[:sort] ||
default_order
end
# Implement sorting_field method on controllers
......@@ -29,42 +32,42 @@ module SortingPreference
private
def set_sort_order_from_user_preference
def set_sort_order_from_user_preference(field = sorting_field)
return unless current_user
return unless sorting_field
return unless field
user_preference = current_user.user_preference
sort_param = params[:sort]
sort_param ||= user_preference[sorting_field]
sort_param ||= user_preference[field]
return sort_param if Gitlab::Database.read_only?
if user_preference[sorting_field] != sort_param
user_preference.update(sorting_field => sort_param)
if user_preference[field] != sort_param
user_preference.update(field => sort_param)
end
sort_param
end
def set_sort_order_from_cookie
def set_sort_order_from_cookie(field = sorting_field)
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]
sort_param ||= cookies[remember_sorting_key]
sort_param ||= cookies[remember_sorting_key(field)]
sort_value = update_cookie_value(sort_param)
set_secure_cookie(remember_sorting_key, sort_value)
set_secure_cookie(remember_sorting_key(field), sort_value)
sort_value
end
# Convert sorting_field to legacy cookie name for backwards compatibility
# :merge_requests_sort => 'mergerequest_sort'
# :issues_sort => 'issue_sort'
def remember_sorting_key
@remember_sorting_key ||= sorting_field
def remember_sorting_key(field = sorting_field)
@remember_sorting_key ||= field
.to_s
.split('_')[0..-2]
.map(&:singularize)
......
......@@ -3,11 +3,14 @@
class Groups::ApplicationController < ApplicationController
include RoutableActions
include ControllerWithCrossProjectAccessCheck
include SortingHelper
include SortingPreference
layout 'group'
skip_before_action :authenticate_user!
before_action :group
before_action :set_sorting
requires_cross_project_access
private
......@@ -57,6 +60,16 @@ class Groups::ApplicationController < ApplicationController
url_for(safe_params)
end
def set_sorting
if has_project_list?
@group_projects_sort = set_sort_order(Project::SORTING_PREFERENCE_FIELD, sort_value_name)
end
end
def has_project_list?
false
end
end
Groups::ApplicationController.prepend_if_ee('EE::Groups::ApplicationController')
......@@ -2,12 +2,15 @@
module Groups
class ChildrenController < Groups::ApplicationController
extend ::Gitlab::Utils::Override
before_action :group
skip_cross_project_access_check :index
feature_category :subgroups
def index
params[:sort] ||= @group_projects_sort
parent = if params[:parent_id].present?
GroupFinder.new(current_user).execute(id: params[:parent_id])
else
......@@ -40,5 +43,12 @@ module Groups
params: params.to_unsafe_h).execute
@children = @children.page(params[:page])
end
private
override :has_project_list?
def has_project_list?
true
end
end
end
......@@ -329,6 +329,11 @@ class GroupsController < Groups::ApplicationController
def markdown_service_params
params.merge(group: group)
end
override :has_project_list?
def has_project_list?
%w(details show index).include?(action_name)
end
end
GroupsController.prepend_if_ee('EE::GroupsController')
......@@ -189,6 +189,10 @@ module GroupsHelper
params.key?(:purchased_quantity) && params[:purchased_quantity].to_i > 0
end
def project_list_sort_by
@group_projects_sort || @sort || params[:sort] || sort_value_recently_created
end
private
def just_created?
......
......@@ -40,6 +40,7 @@ module SortingHelper
sort_value_latest_activity => sort_title_latest_activity,
sort_value_recently_created => sort_title_created_date,
sort_value_name => sort_title_name,
sort_value_name_desc => sort_title_name_desc,
sort_value_stars_desc => sort_title_stars
}
......@@ -95,8 +96,8 @@ module SortingHelper
sort_value_name_desc => sort_title_name_desc,
sort_value_recently_created => sort_title_recently_created,
sort_value_oldest_created => sort_title_oldest_created,
sort_value_recently_updated => sort_title_recently_updated,
sort_value_oldest_updated => sort_title_oldest_updated
sort_value_latest_activity => sort_title_recently_updated,
sort_value_oldest_activity => sort_title_oldest_updated
}
end
......
- options_hash = local_assigns.fetch(:options_hash, groups_sort_options_hash)
- show_archive_options = local_assigns.fetch(:show_archive_options, false)
- if @sort.present?
- default_sort_by = @sort
- else
- if params[:sort]
- default_sort_by = params[:sort]
- else
- default_sort_by = sort_value_recently_created
.dropdown.inline.js-group-filter-dropdown-wrap.gl-mr-3
%button.dropdown-menu-toggle{ type: 'button', 'data-toggle' => 'dropdown' }
%span.dropdown-label
= options_hash[default_sort_by]
= options_hash[project_list_sort_by]
= sprite_icon('chevron-down', css_class: 'dropdown-menu-toggle-icon gl-top-3')
%ul.dropdown-menu.dropdown-menu-right.dropdown-menu-selectable
%li.dropdown-header
= _("Sort by")
- options_hash.each do |value, title|
%li.js-filter-sort-order
= link_to filter_groups_path(sort: value), class: ("is-active" if default_sort_by == value) do
= link_to filter_groups_path(sort: value), class: ("is-active" if project_list_sort_by == value) do
= title
- if show_archive_options
%li.divider
......
---
title: Remember last used project ordering option across groups
merge_request: 47850
author: Lee Tickett
type: changed
......@@ -34,7 +34,7 @@ module Groups
sort_value_start_date_soon
end
def remember_sorting_key
def remember_sorting_key(field = nil)
@remember_sorting_key ||= "roadmap_sort"
end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'User sorts projects and order persists' do
include CookieHelper
let_it_be(:user) { create(:user) }
let_it_be(:group) { create(:group) }
let_it_be(:group_member) { create(:group_member, :maintainer, user: user, group: group) }
let_it_be(:project) { create(:project, :public, group: group) }
shared_examples_for "sort order persists across all views" do |project_paths_label, group_paths_label|
it "is set on the dashboard_projects_path" do
visit(dashboard_projects_path)
expect(find('.dropdown-menu a.is-active', text: project_paths_label)).to have_content(project_paths_label)
end
it "is set on the explore_projects_path" do
visit(explore_projects_path)
expect(find('.dropdown-menu a.is-active', text: project_paths_label)).to have_content(project_paths_label)
end
it "is set on the group_canonical_path" do
visit(group_canonical_path(group))
expect(find('.dropdown-menu a.is-active', text: group_paths_label)).to have_content(group_paths_label)
end
it "is set on the details_group_path" do
visit(details_group_path(group))
expect(find('.dropdown-menu a.is-active', text: group_paths_label)).to have_content(group_paths_label)
end
end
context "from explore projects" do
before do
sign_in(user)
visit(explore_projects_path)
find('#sort-projects-dropdown').click
first(:link, 'Last updated').click
end
it_behaves_like "sort order persists across all views", "Last updated", "Last updated"
end
context 'from dashboard projects' do
before do
sign_in(user)
visit(dashboard_projects_path)
find('#sort-projects-dropdown').click
first(:link, 'Name').click
end
it_behaves_like "sort order persists across all views", "Name", "Name"
end
context 'from group homepage' do
before do
sign_in(user)
visit(group_canonical_path(group))
find('button.dropdown-menu-toggle').click
first(:link, 'Last created').click
end
it_behaves_like "sort order persists across all views", "Created date", "Last created"
end
context 'from group details' do
before do
sign_in(user)
visit(details_group_path(group))
find('button.dropdown-menu-toggle').click
first(:link, 'Most stars').click
end
it_behaves_like "sort order persists across all views", "Stars", "Most stars"
end
end
......@@ -77,6 +77,7 @@ RSpec.describe SortingHelper do
sort_value_latest_activity => sort_title_latest_activity,
sort_value_recently_created => sort_title_created_date,
sort_value_name => sort_title_name,
sort_value_name_desc => sort_title_name_desc,
sort_value_stars_desc => sort_title_stars
}
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment