FEATURE: Add order logic to admin users controller

Added order and direction parameters for sorting admin user pages. This
commit only includes backend api changes.

https://meta.discourse.org/t/make-admin-users-list-sortable-suggestion/47649

Now you can pass in `order` and `asc` parameters to the
`/admin/users/list/<query>.json` endpoint.

Example:

`/admin/users/list/active.json?&order=post_count` which defaults to desc

and

`/admin/users/list/active.json?order=post_count&asc=true`
This commit is contained in:
Blake Erickson
2017-02-24 17:11:17 -07:00
parent fdf749770b
commit 0a41da6bad
3 changed files with 75 additions and 14 deletions

View File

@@ -23,9 +23,24 @@ describe AdminUserIndexQuery do
end
it "allows custom ordering" do
query = ::AdminUserIndexQuery.new({ order: "trust_level DESC" })
query = ::AdminUserIndexQuery.new({ order: "trust_level" })
expect(query.find_users_query.to_sql).to match("trust_level DESC")
end
it "allows custom ordering asc" do
query = ::AdminUserIndexQuery.new({ order: "trust_level", asc: true })
expect(query.find_users_query.to_sql).to match("trust_level ASC" )
end
it "allows custom ordering for stats wtih default direction" do
query = ::AdminUserIndexQuery.new({ order: "topics_entered" })
expect(query.find_users_query.to_sql).to match("topics_entered DESC")
end
it "allows custom ordering and direction for stats" do
query = ::AdminUserIndexQuery.new({ order: "topics_entered", asc: true })
expect(query.find_users_query.to_sql).to match("topics_entered ASC")
end
end
describe "no users with trust level" do
@@ -70,6 +85,28 @@ describe AdminUserIndexQuery do
end
describe "correct order with nil values" do
before(:each) do
Fabricate(:user, email: "test2@example.com", last_emailed_at: 1.hour.ago)
end
it "shows nil values first with asc" do
users = ::AdminUserIndexQuery.new({ order: "last_emailed_at", asc: true }).find_users
expect(users.count).to eq(2)
expect(users.first.username).to eq("system")
expect(users.first.last_emailed_at).to eq(nil)
end
it "shows nil values last with desc" do
users = ::AdminUserIndexQuery.new({ order: "last_emailed_at"}).find_users
expect(users.count).to eq(2)
expect(users.first.last_emailed_at).to_not eq(nil)
end
end
describe "with an admin user" do
let!(:user) { Fabricate(:user, admin: true) }