A platform for community discussion. Free, open, simple.
DEV: Introduce `in:pinned` filter for experimental `/filter` route (#20974)
Alan Guo Xiang Tanab54a616c13b
diff --git a/lib/topics_filter.rb b/lib/topics_filter.rbindex 84cd147aacf..c9222168406 100644--- a/lib/topics_filter.rb+++ b/lib/topics_filter.rb@@ -14,6 +14,8 @@ class TopicsFilter/(?<key_prefix>[-=])?(?<key>\w+):(?<value>[^\s]+)/,) do |key_prefix, key, value|case key+ when "in"+ @scope = filter_state(state: value)when "status"@scope = filter_status(status: value)when "tags"@@ -84,6 +86,18 @@ class TopicsFilterprivate+ def filter_state(state:)+ case state+ when "pinned"+ @scope.where(+ "topics.pinned_at IS NOT NULL AND topics.pinned_until > topics.pinned_at AND ? < topics.pinned_until",+ Time.zone.now,+ )+ else+ @scope+ end+ end+def filter_categories(category_slugs:, exclude_subcategories: false, or_clause: false)category_ids = Category.ids_from_slugs(category_slugs)diff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rbindex fa0c55f9db2..7471669077b 100644--- a/spec/lib/topics_filter_spec.rb+++ b/spec/lib/topics_filter_spec.rb@@ -24,6 +24,40 @@ RSpec.describe TopicsFilter doendend+ describe "when filtering with the `in` filter" do+ fab!(:topic) { Fabricate(:topic) }++ fab!(:pinned_topic) do+ Fabricate(:topic, pinned_at: Time.zone.now, pinned_until: 1.hour.from_now)+ end++ fab!(:expired_pinned_topic) do+ Fabricate(:topic, pinned_at: 2.hour.ago, pinned_until: 1.hour.ago)+ end++ describe "when query string is `in:pinned`" do+ it "should return topics that are pinned" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("in:pinned")+ .pluck(:id),+ ).to contain_exactly(pinned_topic.id)+ end++ it "should not return pinned topics that have expired" do+ freeze_time(2.hours.from_now) do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("in:pinned")+ .pluck(:id),+ ).to eq([])+ end+ end+ end+ end+describe "when filtering by categories" dofab!(:category) { Fabricate(:category, name: "category") }