Skip to content

A platform for community discussion. Free, open, simple.

DEV: Plugin api for saving category attribute (#34898)

Natalie Tayeb40b4f9801e
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb
index 356114ebef1..7dc95e25e26 100644
--- a/app/controllers/categories_controller.rb
+++ b/app/controllers/categories_controller.rb
@@ -547,47 +547,55 @@ class CategoriesController < ApplicationController
conditional_param_keys << { category_localizations: %i[id locale name description] }
end
+ permitted_params = [
+ *required_param_keys,
+ :position,
+ :name,
+ :color,
+ :text_color,
+ :style_type,
+ :emoji,
+ :icon,
+ :email_in,
+ :email_in_allow_strangers,
+ :mailinglist_mirror,
+ :all_topics_wiki,
+ :allow_unlimited_owner_edits_on_first_post,
+ :default_slow_mode_seconds,
+ :parent_category_id,
+ :auto_close_hours,
+ :auto_close_based_on_last_post,
+ :uploaded_logo_id,
+ :uploaded_logo_dark_id,
+ :uploaded_background_id,
+ :uploaded_background_dark_id,
+ :slug,
+ :allow_badges,
+ :topic_template,
+ :sort_order,
+ :sort_ascending,
+ :topic_featured_link_allowed,
+ :show_subcategory_list,
+ :num_featured_topics,
+ :default_view,
+ :subcategory_list_style,
+ :default_top_period,
+ :minimum_required_tags,
+ :navigate_to_first_post_after_read,
+ :search_priority,
+ :allow_global_tags,
+ :read_only_banner,
+ :default_list_filter,
+ *conditional_param_keys,
+ ]
+
+ DiscoursePluginRegistry.category_update_param_with_callback.each do |param_name, config|
+ permitted_params << param_name if config[:plugin].enabled?
+ end
+
result =
params.permit(
- *required_param_keys,
- :position,
- :name,
- :color,
- :text_color,
- :style_type,
- :emoji,
- :icon,
- :email_in,
- :email_in_allow_strangers,
- :mailinglist_mirror,
- :all_topics_wiki,
- :allow_unlimited_owner_edits_on_first_post,
- :default_slow_mode_seconds,
- :parent_category_id,
- :auto_close_hours,
- :auto_close_based_on_last_post,
- :uploaded_logo_id,
- :uploaded_logo_dark_id,
- :uploaded_background_id,
- :uploaded_background_dark_id,
- :slug,
- :allow_badges,
- :topic_template,
- :sort_order,
- :sort_ascending,
- :topic_featured_link_allowed,
- :show_subcategory_list,
- :num_featured_topics,
- :default_view,
- :subcategory_list_style,
- :default_top_period,
- :minimum_required_tags,
- :navigate_to_first_post_after_read,
- :search_priority,
- :allow_global_tags,
- :read_only_banner,
- :default_list_filter,
- *conditional_param_keys,
+ *permitted_params,
category_setting_attributes: %i[
auto_bump_cooldown_days
num_auto_bump_daily
@@ -607,6 +615,17 @@ class CategoriesController < ApplicationController
raise Discourse::InvalidParameters.new(:required_tag_groups)
end
+ if @category
+ DiscoursePluginRegistry.category_update_param_with_callback.each do |param_name, config|
+ next if !config[:plugin].enabled?
+ next if !result.key?(param_name)
+
+ @category.instance_variable_set(:"@#{param_name}_callback_value", result[param_name])
+ # remove from params so that AR doesn't try to set it as an attribute
+ result.delete(param_name)
+ end
+ end
+
result
end
end
diff --git a/app/models/category.rb b/app/models/category.rb
index 31addb9a0ee..f2cb7e81ff7 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -142,6 +142,7 @@ class Category < ActiveRecord::Base
after_update :rename_category_definition, if: :saved_change_to_name?
after_update :create_category_permalink, if: :saved_change_to_slug?
+ after_update :run_plugin_category_update_param_callbacks
after_commit :trigger_category_created_event, on: :create
after_commit :trigger_category_updated_event, on: :update
@@ -1290,6 +1291,22 @@ class Category < ActiveRecord::Base
private
+ def run_plugin_category_update_param_callbacks
+ DiscoursePluginRegistry.category_update_param_with_callback.each do |param_name, opts|
+ next if !opts[:plugin].enabled?
+ next if !instance_variable_defined?(:"@#{param_name}_callback_value")
+
+ value = instance_variable_get(:"@#{param_name}_callback_value")
+ begin
+ opts[:callback].call(self, value)
+ ensure
+ if instance_variable_defined?(:"@#{param_name}_callback_value")
+ remove_instance_variable(:"@#{param_name}_callback_value")
+ end
+ end
+ end
+ end
+
def ensure_category_setting
self.build_category_setting if self.category_setting.blank?
end
diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb
index 86048b68376..af80f8b0631 100644
--- a/lib/discourse_plugin_registry.rb
+++ b/lib/discourse_plugin_registry.rb
@@ -74,6 +74,7 @@ class DiscoursePluginRegistry
define_register :mail_pollers, Set
define_register :site_setting_areas, Set
define_register :discourse_dev_populate_reviewable_types, Set
+ define_register :category_update_param_with_callback, Hash
define_filtered_register :staff_user_custom_fields
define_filtered_register :public_user_custom_fields
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index a5a5b8f943c..b575244f5da 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb
@@ -362,6 +362,27 @@ class Plugin::Instance
Site.add_categories_callbacks(&block)
end
+ # Add a category parameter that includes both controller param permission
+ # and transactional callback. This allows plugins to extend category updates
+ # with custom logic that runs within the same database transaction.
+ #
+ # The callback block receives the category instance and the parameter value.
+ # If the callback raises an exception, the entire category update will be rolled back.
+ #
+ # Example usage:
+ # register_category_update_param_with_callback(:doc_index_topic_id) do |category, value|
+ # DocCategories::CategoryIndexManager.new(category).assign!(value)
+ # end
+ #
+ def register_category_update_param_with_callback(param_name, &callback)
+ reloadable_patch do |plugin|
+ DiscoursePluginRegistry.category_update_param_with_callback[param_name] = {
+ plugin: plugin,
+ callback: callback,
+ }
+ end
+ end
+
def register_upload_unused(&block)
Upload.add_unused_callback(&block)
end
diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb
index 9b3d31a2e56..b1ac7435ba7 100644
--- a/spec/requests/categories_controller_spec.rb
+++ b/spec/requests/categories_controller_spec.rb
@@ -950,6 +950,72 @@ RSpec.describe CategoriesController do
end
end
end
+
+ describe "plugin category parameters" do
+ let(:plugin_instance) { Plugin::Instance.new.tap { |p| p.stubs(:enabled?).returns(true) } }
+ let(:callback) do
+ Proc.new do |category, value|
+ @callback_category = category
+ @callback_value = value
+ end
+ end
+
+ before { sign_in(admin) }
+
+ after { DiscoursePluginRegistry.reset_register!(:category_update_param_with_callback) }
+
+ it "processes plugin parameters when plugin is enabled" do
+ plugin_instance.register_category_update_param_with_callback(:test_param, &callback)
+
+ put "/categories/#{category.id}.json",
+ params: {
+ name: category.name,
+ color: category.color,
+ test_param: "test_value",
+ }
+
+ expect(response.status).to eq(200)
+ expect(@callback_category.id).to eq(category.id)
+ expect(@callback_value).to eq("test_value")
+ end
+
+ it "ignores plugin parameters when plugin is disabled" do
+ plugin_instance.register_category_update_param_with_callback(:test_param, &callback)
+ plugin_instance.stubs(:enabled?).returns(false)
+
+ put "/categories/#{category.id}.json",
+ params: {
+ name: category.name,
+ color: category.color,
+ test_param: "test_value",
+ }
+
+ expect(response.status).to eq(200)
+ expect(@callback_category).to eq(nil)
+ expect(@callback_value).to eq(nil)
+ end
+
+ it "safely handles when attribute is absent" do
+ plugin_instance.register_category_update_param_with_callback(:test_param, &callback)
+
+ put "/categories/#{category.id}.json", params: {}
+
+ expect(response.status).to eq(200)
+ end
+
+ it "rolls back category update if plugin callback fails" do
+ original_name = category.name
+ plugin_instance.register_category_update_param_with_callback(:test_param) do |_|
+ raise "Plugin callback error"
+ end
+
+ put "/categories/#{category.id}.json", params: { test_param: 1 }
+
+ expect(response.status).to eq(500)
+ category.reload
+ expect(category.name).to eq(original_name)
+ end
+ end
end
describe "#update_slug" do