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

This allows multiple ordering to be specified by using a comma seperated string.
For example, `order:created,views` would order the topics by
`Topic#created_at` and then `Topic#views.
This commit is contained in:
Alan Guo Xiang Tan 2023-05-03 12:39:52 +08:00 committed by GitHub
parent 93f7c24240
commit 691b9fb919
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 8 deletions

View File

@ -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

View File

@ -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