Commit 665dd387 authored by Douwe Maan's avatar Douwe Maan

Merge branch '26468-fix-users-sort-in-admin-area' into 'master'

Fixing sort of Users so that users who never logged in will be displayed last

Closes #26468

See merge request !8637
parents 05c198e9 8354c76f
...@@ -179,8 +179,8 @@ class User < ActiveRecord::Base ...@@ -179,8 +179,8 @@ class User < ActiveRecord::Base
scope :not_in_project, ->(project) { project.users.present? ? where("id not in (:ids)", ids: project.users.map(&:id) ) : all } scope :not_in_project, ->(project) { project.users.present? ? where("id not in (:ids)", ids: project.users.map(&:id) ) : all }
scope :without_projects, -> { where('id NOT IN (SELECT DISTINCT(user_id) FROM members WHERE user_id IS NOT NULL AND requested_at IS NULL)') } scope :without_projects, -> { where('id NOT IN (SELECT DISTINCT(user_id) FROM members WHERE user_id IS NOT NULL AND requested_at IS NULL)') }
scope :todo_authors, ->(user_id, state) { where(id: Todo.where(user_id: user_id, state: state).select(:author_id)) } scope :todo_authors, ->(user_id, state) { where(id: Todo.where(user_id: user_id, state: state).select(:author_id)) }
scope :order_recent_sign_in, -> { reorder(last_sign_in_at: :desc) } scope :order_recent_sign_in, -> { reorder(Gitlab::Database.nulls_last_order('last_sign_in_at', 'DESC')) }
scope :order_oldest_sign_in, -> { reorder(last_sign_in_at: :asc) } scope :order_oldest_sign_in, -> { reorder(Gitlab::Database.nulls_last_order('last_sign_in_at', 'ASC')) }
def self.with_two_factor def self.with_two_factor
joins("LEFT OUTER JOIN u2f_registrations AS u2f ON u2f.user_id = users.id"). joins("LEFT OUTER JOIN u2f_registrations AS u2f ON u2f.user_id = users.id").
......
---
title: Fix Sort by Recent Sign-in in Admin Area
merge_request: 8637
author: Poornima M
...@@ -797,14 +797,14 @@ describe User, models: true do ...@@ -797,14 +797,14 @@ describe User, models: true do
describe '#avatar_type' do describe '#avatar_type' do
let(:user) { create(:user) } let(:user) { create(:user) }
it "is true if avatar is image" do it 'is true if avatar is image' do
user.update_attribute(:avatar, 'uploads/avatar.png') user.update_attribute(:avatar, 'uploads/avatar.png')
expect(user.avatar_type).to be_truthy expect(user.avatar_type).to be_truthy
end end
it "is false if avatar is html page" do it 'is false if avatar is html page' do
user.update_attribute(:avatar, 'uploads/avatar.html') user.update_attribute(:avatar, 'uploads/avatar.html')
expect(user.avatar_type).to eq(["only images allowed"]) expect(user.avatar_type).to eq(['only images allowed'])
end end
end end
...@@ -926,8 +926,8 @@ describe User, models: true do ...@@ -926,8 +926,8 @@ describe User, models: true do
end end
end end
describe "#starred?" do describe '#starred?' do
it "determines if user starred a project" do it 'determines if user starred a project' do
user = create :user user = create :user
project1 = create(:empty_project, :public) project1 = create(:empty_project, :public)
project2 = create(:empty_project, :public) project2 = create(:empty_project, :public)
...@@ -953,8 +953,8 @@ describe User, models: true do ...@@ -953,8 +953,8 @@ describe User, models: true do
end end
end end
describe "#toggle_star" do describe '#toggle_star' do
it "toggles stars" do it 'toggles stars' do
user = create :user user = create :user
project = create(:empty_project, :public) project = create(:empty_project, :public)
...@@ -966,31 +966,44 @@ describe User, models: true do ...@@ -966,31 +966,44 @@ describe User, models: true do
end end
end end
describe "#sort" do describe '#sort' do
before do before do
User.delete_all User.delete_all
@user = create :user, created_at: Date.today, last_sign_in_at: Date.today, name: 'Alpha' @user = create :user, created_at: Date.today, last_sign_in_at: Date.today, name: 'Alpha'
@user1 = create :user, created_at: Date.today - 1, last_sign_in_at: Date.today - 1, name: 'Omega' @user1 = create :user, created_at: Date.today - 1, last_sign_in_at: Date.today - 1, name: 'Omega'
@user2 = create :user, created_at: Date.today - 2, last_sign_in_at: nil, name: 'Beta'
end end
it "sorts users by the recent sign-in time" do context 'when sort by recent_sign_in' do
it 'sorts users by the recent sign-in time' do
expect(User.sort('recent_sign_in').first).to eq(@user) expect(User.sort('recent_sign_in').first).to eq(@user)
end end
it "sorts users by the oldest sign-in time" do it 'pushes users who never signed in to the end' do
expect(User.sort('recent_sign_in').third).to eq(@user2)
end
end
context 'when sort by oldest_sign_in' do
it 'sorts users by the oldest sign-in time' do
expect(User.sort('oldest_sign_in').first).to eq(@user1) expect(User.sort('oldest_sign_in').first).to eq(@user1)
end end
it "sorts users in descending order by their creation time" do it 'pushes users who never signed in to the end' do
expect(User.sort('oldest_sign_in').third).to eq(@user2)
end
end
it 'sorts users in descending order by their creation time' do
expect(User.sort('created_desc').first).to eq(@user) expect(User.sort('created_desc').first).to eq(@user)
end end
it "sorts users in ascending order by their creation time" do it 'sorts users in ascending order by their creation time' do
expect(User.sort('created_asc').first).to eq(@user1) expect(User.sort('created_asc').first).to eq(@user2)
end end
it "sorts users by id in descending order when nil is passed" do it 'sorts users by id in descending order when nil is passed' do
expect(User.sort(nil).first).to eq(@user1) expect(User.sort(nil).first).to eq(@user2)
end end
end 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