Skip to content

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

DEV: Support comma seperated value in order filter for `/filter` route (#21318)

Alan Guo Xiang Tan691b9fb919ae
diff --git a/lib/topics_filter.rb b/lib/topics_filter.rb
index a11587c1120..f859af5d0b9 100644
--- a/lib/topics_filter.rb
+++ b/lib/topics_filter.rb
@@ -132,6 +132,8 @@ class TopicsFilter
"posters-min", "posters-max", "views-min", "views-max"
value = values.last
value if value =~ /\A\d+\z/
+ when "order"
+ values.flat_map { |value| value.split(",") }
when "created-by"
values.flat_map { |value| value.split(",").map { |username| username.delete_prefix("@") } }
else
diff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rb
index f1e38d4e519..24d56c63695 100644
--- a/spec/lib/topics_filter_spec.rb
+++ b/spec/lib/topics_filter_spec.rb
@@ -1209,18 +1209,31 @@ RSpec.describe TopicsFilter do
end
end
- describe "when query string is `order:created order:views`" do
+ 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) }
fab!(:topic3) { Fabricate(:topic, created_at: Time.zone.local(2024, 1, 1), views: 1) }
- it "should return topics ordered by creation date in descending order and then number of views in descending order" do
- expect(
- TopicsFilter
- .new(guardian: Guardian.new)
- .filter_from_query_string("order:created order:views")
- .pluck(:id),
- ).to eq([topic2.id, topic3.id, topic.id])
+ describe "when query string is `order:created,views`" do
+ it "should return topics ordered by creation date in descending order and then number of views in descending order" do
+ expect(
+ TopicsFilter
+ .new(guardian: Guardian.new)
+ .filter_from_query_string("order:created,views")
+ .pluck(:id),
+ ).to eq([topic2.id, topic3.id, topic.id])
+ end
+ end
+
+ describe "when query string is `order:created order:views`" do
+ it "should return topics ordered by creation date in descending order and then number of views in descending order" do
+ expect(
+ TopicsFilter
+ .new(guardian: Guardian.new)
+ .filter_from_query_string("order:created order:views")
+ .pluck(:id),
+ ).to eq([topic2.id, topic3.id, topic.id])
+ end
end
end
end