DEV: Introduced topic_query_create_list_topics modifier (#21016)

Introduced a modifier on topic_query to change list while they're created

Co-authored-by: Jarek Radosz <jradosz@gmail.com>
This commit is contained in:
Sérgio Saquetim 2023-04-07 13:01:42 -03:00 committed by GitHub
parent 18470c9195
commit f8fb7ee9f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -460,6 +460,8 @@ class TopicQuery
options[:filter] ||= filter
topics ||= default_results(options)
topics = yield(topics) if block_given?
topics =
DiscoursePluginRegistry.apply_modifier(:topic_query_create_list_topics, topics, options, self)
options = options.merge(@options)
if %w[activity default].include?(options[:order] || "activity") && !options[:unordered] &&

View File

@ -1940,4 +1940,29 @@ RSpec.describe TopicQuery do
).to contain_exactly(category.topic_id, subcategory.topic_id, subcategory_regular_topic.id)
end
end
describe "with topic_query_create_list_topics modifier" do
fab!(:topic1) { Fabricate(:topic, created_at: 3.days.ago, bumped_at: 1.hour.ago) }
fab!(:topic2) { Fabricate(:topic, created_at: 2.days.ago, bumped_at: 3.hour.ago) }
after { DiscoursePluginRegistry.clear_modifiers! }
it "allows changing" do
original_topic_query = TopicQuery.new(user)
Plugin::Instance
.new
.register_modifier(:topic_query_create_list_topics) do |topics, options, topic_query|
expect(topic_query).to eq(topic_query)
topic_query.options[:order] = "created"
topics
end
expect(original_topic_query.list_latest.topics.map(&:id)).to eq([topic1, topic2].map(&:id))
DiscoursePluginRegistry.clear_modifiers!
expect(original_topic_query.list_latest.topics.map(&:id)).to eq([topic2, topic1].map(&:id))
end
end
end