mirror of
https://github.com/discourse/discourse.git
synced 2025-03-03 09:49:42 +00:00
DEV: add support for adding custom status filter
Those can be used in the /filter routes.
This commit is contained in:
parent
351d212e8a
commit
803c275bd7
@ -236,6 +236,14 @@ class Plugin::Instance
|
|||||||
DiscoursePluginRegistry.register_editable_group_custom_field(field, self)
|
DiscoursePluginRegistry.register_editable_group_custom_field(field, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Allows to define custom "status:" filter. Example usage:
|
||||||
|
# register_custom_filter_by_status("foobar") do |scope|
|
||||||
|
# scope.where("word_count = 42")
|
||||||
|
# end
|
||||||
|
def register_custom_filter_by_status(status, &block)
|
||||||
|
TopicsFilter.add_filter_by_status(status, &block)
|
||||||
|
end
|
||||||
|
|
||||||
# Allows to define custom search order. Example usage:
|
# Allows to define custom search order. Example usage:
|
||||||
# Search.advanced_order(:chars) do |posts|
|
# Search.advanced_order(:chars) do |posts|
|
||||||
# posts.reorder("(SELECT LENGTH(raw) FROM posts WHERE posts.topic_id = subquery.topic_id) DESC")
|
# posts.reorder("(SELECT LENGTH(raw) FROM posts WHERE posts.topic_id = subquery.topic_id) DESC")
|
||||||
|
@ -86,6 +86,14 @@ class TopicsFilter
|
|||||||
@scope
|
@scope
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.add_filter_by_status(status, &blk)
|
||||||
|
(@custom_status_filters ||= {})[status] = blk
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.custom_status_filters
|
||||||
|
@custom_status_filters || {}
|
||||||
|
end
|
||||||
|
|
||||||
def filter_status(status:, category_id: nil)
|
def filter_status(status:, category_id: nil)
|
||||||
case status
|
case status
|
||||||
when "open"
|
when "open"
|
||||||
@ -106,6 +114,10 @@ class TopicsFilter
|
|||||||
end
|
end
|
||||||
when "public"
|
when "public"
|
||||||
@scope = @scope.joins(:category).where("NOT categories.read_restricted")
|
@scope = @scope.joins(:category).where("NOT categories.read_restricted")
|
||||||
|
else
|
||||||
|
if custom_filter = TopicsFilter.custom_status_filters[status]
|
||||||
|
@scope = custom_filter.call(@scope)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@scope
|
@scope
|
||||||
|
@ -617,6 +617,18 @@ RSpec.describe TopicsFilter do
|
|||||||
fab!(:closed_topic) { Fabricate(:topic, closed: true) }
|
fab!(:closed_topic) { Fabricate(:topic, closed: true) }
|
||||||
fab!(:archived_topic) { Fabricate(:topic, archived: true) }
|
fab!(:archived_topic) { Fabricate(:topic, archived: true) }
|
||||||
fab!(:deleted_topic_id) { Fabricate(:topic, deleted_at: Time.zone.now).id }
|
fab!(:deleted_topic_id) { Fabricate(:topic, deleted_at: Time.zone.now).id }
|
||||||
|
fab!(:foobar_topic) { Fabricate(:topic, closed: true, word_count: 42) }
|
||||||
|
|
||||||
|
it "supports custom status filters" do
|
||||||
|
TopicsFilter.add_filter_by_status("foobar") { |scope| scope.where("word_count = 42") }
|
||||||
|
|
||||||
|
expect(
|
||||||
|
TopicsFilter
|
||||||
|
.new(guardian: Guardian.new)
|
||||||
|
.filter_from_query_string("status:foobar")
|
||||||
|
.pluck(:id),
|
||||||
|
).to contain_exactly(foobar_topic.id)
|
||||||
|
end
|
||||||
|
|
||||||
it "should only return topics that have not been closed or archived when query string is `status:open`" do
|
it "should only return topics that have not been closed or archived when query string is `status:open`" do
|
||||||
expect(
|
expect(
|
||||||
@ -642,7 +654,7 @@ RSpec.describe TopicsFilter do
|
|||||||
.new(guardian: Guardian.new)
|
.new(guardian: Guardian.new)
|
||||||
.filter_from_query_string("status:deleted")
|
.filter_from_query_string("status:deleted")
|
||||||
.pluck(:id),
|
.pluck(:id),
|
||||||
).to contain_exactly(topic.id, closed_topic.id, archived_topic.id)
|
).to contain_exactly(topic.id, closed_topic.id, archived_topic.id, foobar_topic.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should only return topics that have been archived when query string is `status:archived`" do
|
it "should only return topics that have been archived when query string is `status:archived`" do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user