Skip to content

A platform for community discussion. Free, open, simple.

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

Alan Guo Xiang Tan6c838c73e545
diff --git a/lib/topics_filter.rb b/lib/topics_filter.rb
index c12052c5a3f..5ffa80319f1 100644
--- a/lib/topics_filter.rb
+++ b/lib/topics_filter.rb
@@ -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
diff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rb
index c8000626f8d..fa4681380bc 100644
--- a/spec/lib/topics_filter_spec.rb
+++ b/spec/lib/topics_filter_spec.rb
@@ -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) }