A platform for community discussion. Free, open, simple.
DEV: cleanup custom filters to prevent leaks
Régis Hanolf7a1272fa48f
diff --git a/lib/search.rb b/lib/search.rbindex 557a2bb6c88..1a388870243 100644--- a/lib/search.rb+++ b/lib/search.rb@@ -349,19 +349,19 @@ class Searchenddef self.advanced_order(trigger, &block)- (@advanced_orders ||= {})[trigger] = block+ advanced_orders[trigger] = blockenddef self.advanced_orders- @advanced_orders+ @advanced_orders ||= {}enddef self.advanced_filter(trigger, &block)- (@advanced_filters ||= {})[trigger] = block+ advanced_filters[trigger] = blockenddef self.advanced_filters- @advanced_filters+ @advanced_filters ||= {}enddef self.custom_topic_eager_load(tables = nil, &block)diff --git a/lib/topic_view.rb b/lib/topic_view.rbindex b59ba0f1543..ce221e50d24 100644--- a/lib/topic_view.rb+++ b/lib/topic_view.rb@@ -73,12 +73,11 @@ class TopicViewenddef self.add_custom_filter(key, &blk)- @custom_filters ||= {}- @custom_filters[key] = blk+ custom_filters[key] = blkenddef self.custom_filters- @custom_filters || {}+ @custom_filters ||= {}end# Configure a default scope to be applied to @filtered_posts.diff --git a/lib/topics_filter.rb b/lib/topics_filter.rbindex 4d075d53ac0..e1cefed5112 100644--- a/lib/topics_filter.rb+++ b/lib/topics_filter.rb@@ -87,11 +87,11 @@ class TopicsFilterenddef self.add_filter_by_status(status, &blk)- (@custom_status_filters ||= {})[status] = blk+ custom_status_filters[status] = blkenddef self.custom_status_filters- @custom_status_filters || {}+ @custom_status_filters ||= {}enddef filter_status(status:, category_id: nil)diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rbindex 3e8d9d592fd..79f18740124 100644--- a/spec/lib/search_spec.rb+++ b/spec/lib/search_spec.rb@@ -2635,11 +2635,18 @@ RSpec.describe Search doendlet!(:post1) { Fabricate(:post, raw: "this is the second post about advanced filter") }+ after do+ Search.advanced_filters.clear+ Search.advanced_orders.clear+ end+it "allows to define custom filter" doexpect(Search.new("advanced").execute.posts).to eq([post1, post0])+Search.advanced_filter(/^min_chars:(\d+)$/) do |posts, match|posts.where("(SELECT LENGTH(p2.raw) FROM posts p2 WHERE p2.id = posts.id) >= ?", match.to_i)end+expect(Search.new("advanced min_chars:50").execute.posts).to eq([post0])enddiff --git a/spec/lib/topic_view_spec.rb b/spec/lib/topic_view_spec.rbindex cdc67944f55..15660e0e08d 100644--- a/spec/lib/topic_view_spec.rb+++ b/spec/lib/topic_view_spec.rb@@ -72,6 +72,8 @@ RSpec.describe TopicView dofab!(:p0) { Fabricate(:post, topic: topic) }fab!(:p1) { Fabricate(:post, topic: topic, wiki: true) }+ after { TopicView.custom_filters.clear }+it "allows to register custom filters" dotv = TopicView.new(topic.id, evil_trout, { filter: "wiki" })expect(tv.filter_posts({ filter: "wiki" })).to eq([p0, p1])@@ -83,8 +85,6 @@ RSpec.describe TopicView dotv = TopicView.new(topic.id, evil_trout, { filter: "whatever" })expect(tv.filter_posts).to eq([p0, p1])- ensure- TopicView.instance_variable_set(:@custom_filters, {})endenddiff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rbindex 64247ba74a5..1ba3e496e57 100644--- a/spec/lib/topics_filter_spec.rb+++ b/spec/lib/topics_filter_spec.rb@@ -619,6 +619,8 @@ RSpec.describe TopicsFilter dofab!(:deleted_topic_id) { Fabricate(:topic, deleted_at: Time.zone.now).id }fab!(:foobar_topic) { Fabricate(:topic, closed: true, word_count: 42) }+ after { TopicsFilter.custom_status_filters.clear }+it "supports custom status filters" doTopicsFilter.add_filter_by_status("foobar") { |scope| scope.where("word_count = 42") }diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 9c1c3d1bc2b..a2afd121fec 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -3336,6 +3336,9 @@ RSpec.describe TopicsController dodescribe "custom filters" dofab!(:post2) { Fabricate(:post, user: post_author2, topic: topic, percent_rank: 0.2) }fab!(:post3) { Fabricate(:post, user: post_author3, topic: topic, percent_rank: 0.5) }++ after { TopicView.custom_filters.clear }+it "should return the right posts" doTopicView.add_custom_filter("percent") do |posts, topic_view|posts.where(percent_rank: 0.5)@@ -3348,8 +3351,6 @@ RSpec.describe TopicsController dobody = response.parsed_bodyexpect(body["post_stream"]["posts"].map { |p| p["id"] }).to eq([post3.id])- ensure- TopicView.instance_variable_set(:@custom_filters, {})endendend