A platform for community discussion. Free, open, simple.
Revert "DEV: Allow specifying a condition when preloading topics for … (#32092)
Joffrey JAFFEUXb21c3e86f7bf
diff --git a/app/models/topic_list.rb b/app/models/topic_list.rbindex ed91667b5c1..9578eb11310 100644--- a/app/models/topic_list.rb+++ b/app/models/topic_list.rb@@ -138,13 +138,7 @@ class TopicList{ category: :parent_category },]- DiscoursePluginRegistry.topic_preloader_associations.each do |assoc|- if assoc.is_a?(Symbol)- topic_preloader_associations << assoc- elsif assoc.is_a?(Hash) && assoc[:condition].call- topic_preloader_associations << assoc[:association]- end- end+ topic_preloader_associations.concat(DiscoursePluginRegistry.topic_preloader_associations.to_a)ActiveRecord::Associations::Preloader.new(records: @topics,diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rbindex dbc31c71c87..d1f292afb66 100644--- a/lib/plugin/instance.rb+++ b/lib/plugin/instance.rb@@ -1347,24 +1347,6 @@ class Plugin::Instanceendend- # This method allows plugins to preload topic associations when loading topics- # that make use of topic_list.- #- # @param fields [Array<Symbol>, Symbol, Hash] The topic associations to preload.- # - :association` [Symbol] The association to preload.- # - :condition` [Proc] A function that returns a boolean value indicating whether the association should be preloaded- #- # @example- # register_topic_preloader_associations(%i[custom_association another_association])- # register_topic_preloader_associations({- # association: :linked_topic, condition: ->(topic) { topic.custom_field.present? }- # })- #- # @return [void]- def register_topic_preloader_associations(fields)- DiscoursePluginRegistry.register_topic_preloader_association(fields, self)- end-protecteddef self.js_path@@ -1459,6 +1441,10 @@ class Plugin::Instancereloadable_patch { NewPostManager.add_plugin_payload_attribute(attribute_name) }end+ def register_topic_preloader_associations(fields)+ DiscoursePluginRegistry.register_topic_preloader_association(fields, self)+ end+### Allows plugins to preload topic associations when loading categories with topics.#diff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rbindex 468975abbf0..6ac2fed86cb 100644--- a/spec/lib/topic_query_spec.rb+++ b/spec/lib/topic_query_spec.rb@@ -1309,14 +1309,6 @@ RSpec.describe TopicQuery do:first_post,Plugin::Instance.new,)- DiscoursePluginRegistry.register_topic_preloader_association(- { association: :user_warning, condition: -> { true } },- Plugin::Instance.new,- )- DiscoursePluginRegistry.register_topic_preloader_association(- { association: :linked_topic, condition: -> { false } },- Plugin::Instance.new,- )topic = Fabricate(:topic)Fabricate(:post, topic: topic)@@ -1324,8 +1316,6 @@ RSpec.describe TopicQuery donew_topic = topic_query.list_new.topics.firstexpect(new_topic.association(:image_upload).loaded?).to eq(true) # Preloaded by defaultexpect(new_topic.association(:first_post).loaded?).to eq(true) # Testing a user-defined preloaded association- expect(new_topic.association(:user_warning).loaded?).to eq(true) # Conditionally loaded- expect(new_topic.association(:linked_topic).loaded?).to eq(false) # Failed conditionexpect(new_topic.association(:user).loaded?).to eq(false) # Testing the negativeDiscoursePluginRegistry.reset_register!(:topic_preloader_associations)