DEV: Add `order:title` filter to experimental `/filter` route (#22293)

This has been requested multiple times by users so it makes sense for us
to support this as well.

See https://meta.discourse.org/t/sort-display-of-topics-alphabetically/53911
This commit is contained in:
Alan Guo Xiang Tan 2023-06-28 06:21:56 +08:00 committed by GitHub
parent d3facec7d0
commit 6c838c73e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -490,6 +490,9 @@ class TopicsFilter
"posters" => {
column: "topics.participant_count",
},
"title" => {
column: "LOWER(topics.title)",
},
"views" => {
column: "topics.views",
},
@ -508,7 +511,7 @@ class TopicsFilter
@scope = instance_exec(&scope)
end
@scope = @scope.order(column_name => match_data[:asc] ? :asc : :desc)
@scope = @scope.order("#{column_name} #{match_data[:asc] ? "ASC" : "DESC"}")
end
end
end

View File

@ -1336,6 +1336,14 @@ RSpec.describe TopicsFilter do
include_examples "ordering topics filters", "category", "category name"
end
describe "when ordering by topics's title" do
fab!(:topic3) { Fabricate(:topic, title: "This is topic number 1") }
fab!(:topic2) { Fabricate(:topic, title: "This is topic Number 3") }
fab!(:topic) { Fabricate(:topic, title: "This is topic number 2") }
include_examples "ordering topics filters", "title", "topic's title"
end
describe "composing multiple order filters" do
fab!(:topic) { Fabricate(:topic, created_at: Time.zone.local(2023, 1, 1), views: 2) }
fab!(:topic2) { Fabricate(:topic, created_at: Time.zone.local(2024, 1, 1), views: 2) }