A platform for community discussion. Free, open, simple.
DEV: Fix `order:` filter not working on `/filter` route (#21330)
Alan Guo Xiang Tanc12e7112bf52
diff --git a/lib/topic_query.rb b/lib/topic_query.rbindex 98c54597066..ddb15e1ce40 100644--- a/lib/topic_query.rb+++ b/lib/topic_query.rb@@ -270,7 +270,10 @@ class TopicQuerydef list_filtertopics_filter =- TopicsFilter.new(guardian: @guardian, scope: latest_results(include_muted: false))+ TopicsFilter.new(+ guardian: @guardian,+ scope: latest_results(include_muted: false, skip_ordering: true),+ )results = topics_filter.filter_from_query_string(@options[:q])@@ -278,6 +281,8 @@ class TopicQueryresults = remove_muted_topics(results, @user)end+ results = apply_ordering(results) if results.order_values.empty?+create_list(:filter, {}, results)end@@ -618,7 +623,7 @@ class TopicQueryresult.where("topics.category_id != ?", drafts_category_id)end- def apply_ordering(result, options)+ def apply_ordering(result, options = {})sort_column = SORTABLE_MAPPING[options[:order]] || "default"sort_dir = (options[:ascending] == "true") ? "ASC" : "DESC"@@ -732,7 +737,7 @@ class TopicQueryresult = filter_by_tags(result)end- result = apply_ordering(result, options)+ result = apply_ordering(result, options) if !options[:skip_ordering]all_listable_topics =@guardian.filter_allowed_categories(diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rbindex 66f7fb1a37e..04f3aa5e19e 100644--- a/spec/requests/list_controller_spec.rb+++ b/spec/requests/list_controller_spec.rb@@ -1308,6 +1308,36 @@ RSpec.describe ListController doendend+ describe "when ordering using the `order:` filter" do+ fab!(:topic2) { Fabricate(:topic, views: 2) }+ fab!(:topic3) { Fabricate(:topic, views: 3) }+ fab!(:topic4) { Fabricate(:topic, views: 1) }++ it "return topics ordered by topic bumped at date in descending order when `q` query param is not present" do+ sign_in(user)++ get "/filter.json"++ expect(response.status).to eq(200)++ expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq(+ [topic4.id, topic3.id, topic2.id, topic.id],+ )+ end++ it "return topics ordered by views when `q` query param is `order:views`" do+ sign_in(user)++ get "/filter.json", params: { q: "order:views" }++ expect(response.status).to eq(200)++ expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq(+ [topic3.id, topic2.id, topic4.id, topic.id],+ )+ end+ end+describe "when filtering by status" dofab!(:group) { Fabricate(:group) }fab!(:private_category) { Fabricate(:private_category, group: group) }