From 1d5060dec4e0b708a356d731544ff2e4d3dd79f1 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 25 Aug 2023 16:41:03 +0100 Subject: [PATCH] FIX: Respect default category sort order when navigating within app When navigating around, we make ajax requests with a parameter like `?filter=latest`. This results in the TopicQuery being set up with `filter: "latest"` as a string. The logic introduced in fd9a5bc0 checks for equality with `:latest` and `:unseen` symbols, which didn't work correctly in this situation This commit makes the logic detect both strings and symbols, and adds a spec for the behaviour. --- lib/topic_query.rb | 2 +- spec/lib/topic_query_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/topic_query.rb b/lib/topic_query.rb index c0f8aeacfcc709..a8c3610650cd25 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -749,7 +749,7 @@ def default_results(options = {}) # category default sort order sort_order, sort_ascending = Category.where(id: category_id).pick(:sort_order, :sort_ascending) - if sort_order && (filter.blank? || %i[latest unseen].include?(filter)) + if sort_order && (filter.blank? || %w[latest unseen].include?(filter.to_s)) options[:order] = sort_order options[:ascending] = !!sort_ascending ? "true" : "false" else diff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rb index e1259fe8def613..b7a14f0f732dec 100644 --- a/spec/lib/topic_query_spec.rb +++ b/spec/lib/topic_query_spec.rb @@ -998,6 +998,13 @@ class ::TopicQuery expect(topic_ids - [topic_category.id]).to eq([topic_in_cat1.id, topic_in_cat2.id]) end + it "uses the category's default sort order when filter is passed as a string" do + category.update!(sort_order: "created", sort_ascending: true) + topic_ids = + TopicQuery.new(user, category: category.id, filter: "latest").list_latest.topics.map(&:id) + expect(topic_ids - [topic_category.id]).to eq([topic_in_cat1.id, topic_in_cat2.id]) + end + it "should apply default sort order to latest and unseen filters only" do category.update!(sort_order: "created", sort_ascending: true)