Skip to content

Commit 7956984

Browse files
committed
Order users list in the database rather than in memory
1 parent b604217 commit 7956984

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

app/controllers/users_controller.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ class UsersController < ApplicationController
99
def index
1010
authorize current_user, :can_manage_user?
1111

12-
roles = User.roles.keys
13-
@users = policy_scope(User).sort_by do |user|
14-
[
15-
user.organisation&.name || "",
16-
user.has_access ? 0 : 1,
17-
roles.index(user.role),
18-
user.name || "",
19-
]
20-
end
12+
@users = policy_scope(User).for_users_list
2113

2214
render template: "users/index", locals: { users: @users }
2315
end

app/models/user.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ class UserAuthenticationException < StandardError; end
2727
standard: "standard",
2828
}
2929

30+
scope :for_users_list, lambda {
31+
includes(:organisation)
32+
.order(Arel.sql("organisation.name ASC NULLS FIRST"))
33+
.order({ has_access: :desc })
34+
.in_order_of(:role, roles.keys)
35+
.order({ name: :asc })
36+
}
37+
3038
validates :name, presence: true, if: :requires_name?
3139
validates :role, presence: true
3240
validates :organisation_id, presence: true, if: :requires_organisation?

0 commit comments

Comments
 (0)