A platform for community discussion. Free, open, simple.
FEATURE: allow date-based filters to accept a day count (#33197)
Krisbc4844cb93d1
diff --git a/lib/topics_filter.rb b/lib/topics_filter.rbindex f5b280cdc8b..d818c20f362 100644--- a/lib/topics_filter.rb+++ b/lib/topics_filter.rb@@ -145,6 +145,11 @@ class TopicsFilterTime.zone.parse("#{match_data[:year].to_i}-#{match_data[:month].to_i}-#{match_data[:day].to_i}",)+ elsif value =~ /\A\d+\z/+ # Handle integer as number of days ago (0 = today at midnight)+ days = value.to_i+ return nil if days < 0+ days.days.ago.beginning_of_dayendwhen "likes-min", "likes-max", "likes-op-min", "likes-op-max", "posts-min", "posts-max","posters-min", "posters-max", "views-min", "views-max"diff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rbindex 77e14b652d1..d30cba1531a 100644--- a/spec/lib/topics_filter_spec.rb+++ b/spec/lib/topics_filter_spec.rb@@ -1331,6 +1331,56 @@ RSpec.describe TopicsFilter do).to eq([])endend++ describe "when query string is `#{filter}-after:1`" do+ it "should only return topics with #{description} after 1 day ago" do+ freeze_time do+ old_topic = Fabricate(:topic, column => 2.days.ago)+ recent_topic = Fabricate(:topic, column => Time.zone.now)++ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("#{filter}-after:1")+ .pluck(:id),+ ).to contain_exactly(recent_topic.id)+ end+ end+ end++ describe "when query string is `#{filter}-before:1`" do+ it "should only return topics with #{description} before 1 day ago" do+ freeze_time do+ old_topic = Fabricate(:topic, column => 2.days.ago)+ recent_topic = Fabricate(:topic, column => Time.zone.now)++ results =+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("#{filter}-before:1")+ .where(id: [old_topic.id, recent_topic.id])+ .pluck(:id)++ expect(results).to contain_exactly(old_topic.id)+ end+ end+ end++ describe "when query string is `#{filter}-after:0`" do+ it "should only return topics with #{description} after today" do+ freeze_time do+ old_topic = Fabricate(:topic, column => 2.days.ago)+ recent_topic = Fabricate(:topic, column => Time.zone.now)++ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("#{filter}-after:0")+ .pluck(:id),+ ).to contain_exactly(recent_topic.id)+ end+ end+ endenddescribe "when filtering by activity of topics" do