Skip to content

Commit

Permalink
FIX: Rely on core for staff action logs (#176)
Browse files Browse the repository at this point in the history
Previously, we were deleting the `enable_topic_voting` custom field param
and doing the staff logging in this plugin. This has a problem though, it
was being done in the model's create/destroy hooks and was missing the
acting user (so the logs would report the change as `system`).

This PR keeps the custom field param and relies on core to do the staff
action logging. This change also results in the custom field being set
in the DB when it is enabled even though we don't use it in the serializer.
  • Loading branch information
pmusaraj committed Dec 18, 2023
1 parent c1195c3 commit be71ec4
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 45 deletions.
26 changes: 0 additions & 26 deletions app/models/discourse_topic_voting/category_setting.rb
Expand Up @@ -8,8 +8,6 @@ class CategorySetting < ActiveRecord::Base

before_create :unarchive_votes
before_destroy :archive_votes
after_create :log_category_voting_enabled
after_destroy :log_category_voting_disabled
after_save :reset_voting_cache

def unarchive_votes
Expand Down Expand Up @@ -38,30 +36,6 @@ def archive_votes
def reset_voting_cache
::Category.reset_voting_cache
end

def log_category_voting_disabled
::StaffActionLogger.new(Discourse.system_user).log_category_settings_change(
self.category,
{ custom_fields: { enable_topic_voting: "false" } },
old_permissions: {
},
old_custom_fields: {
enable_topic_voting: "true",
},
)
end

def log_category_voting_enabled
::StaffActionLogger.new(Discourse.system_user).log_category_settings_change(
self.category,
{ custom_fields: { enable_topic_voting: "true" } },
old_permissions: {
},
old_custom_fields: {
enable_topic_voting: "false",
},
)
end
end
end

Expand Down
Expand Up @@ -4,7 +4,7 @@ module DiscourseTopicVoting
module CategoriesControllerExtension
def category_params
@vote_enabled ||=
!!ActiveRecord::Type::Boolean.new.cast(params[:custom_fields]&.delete(:enable_topic_voting))
!!ActiveRecord::Type::Boolean.new.cast(params[:custom_fields][:enable_topic_voting])

category_params = super

Expand Down
1 change: 1 addition & 0 deletions plugin.rb
Expand Up @@ -96,6 +96,7 @@ class Engine < ::Rails::Engine
end
end

register_category_custom_field_type("enable_topic_voting", :boolean)
add_to_serializer(:category, :custom_fields, respect_plugin_enabled: false) do
return object.custom_fields if !SiteSetting.voting_enabled

Expand Down
18 changes: 0 additions & 18 deletions spec/models/discourse_voting/category_setting_spec.rb

This file was deleted.

0 comments on commit be71ec4

Please sign in to comment.