From d1201d618832fefa290760447a8125ca5ca159ff Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Fri, 22 Oct 2021 10:22:09 +0800 Subject: [PATCH] DEV: Pass topic to `TopicView.add_post_custom_fields_allowlister` (#14678) Allows custom fields to be loaded based on the attributes of a topic. --- lib/plugin/instance.rb | 4 ++-- lib/topic_view.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index f7e9305e34f..7158f0d3c1a 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -326,8 +326,8 @@ class Plugin::Instance # Add a post_custom_fields_allowlister block to the TopicView, respecting if the plugin is enabled def topic_view_post_custom_fields_allowlister(&block) reloadable_patch do |plugin| - ::TopicView.add_post_custom_fields_allowlister do |user| - plugin.enabled? ? block.call(user) : [] + ::TopicView.add_post_custom_fields_allowlister do |user, topic| + plugin.enabled? ? block.call(user, topic) : [] end end end diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 1f2cb0103b3..7892c2db580 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -65,8 +65,8 @@ class TopicView post_custom_fields_allowlisters << block end - def self.allowed_post_custom_fields(user) - wpcf = default_post_custom_fields + post_custom_fields_allowlisters.map { |w| w.call(user) } + def self.allowed_post_custom_fields(user, topic) + wpcf = default_post_custom_fields + post_custom_fields_allowlisters.map { |w| w.call(user, topic) } wpcf.flatten.uniq end @@ -116,7 +116,7 @@ class TopicView @user_custom_fields = User.custom_fields_for_ids(@posts.pluck(:user_id), added_fields) end - if (allowed_fields = TopicView.allowed_post_custom_fields(@user)).present? + if (allowed_fields = TopicView.allowed_post_custom_fields(@user, @topic)).present? @post_custom_fields = Post.custom_fields_for_ids(@posts.pluck(:id), allowed_fields) end end