mirror of
https://github.com/discourse/discourse.git
synced 2025-02-16 18:24:52 -06:00
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:
parent
93f7c24240
commit
691b9fb919
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user