FIX: Rely on core for staff action logs (#176)
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.
This commit is contained in:
parent
c1195c30f8
commit
be71ec457c
|
@ -8,8 +8,6 @@ module DiscourseTopicVoting
|
|||
|
||||
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
|
||||
|
@ -38,30 +36,6 @@ module DiscourseTopicVoting
|
|||
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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -96,6 +96,7 @@ after_initialize do
|
|||
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
|
||||
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
require "rails_helper"
|
||||
|
||||
describe DiscourseTopicVoting::CategorySetting do
|
||||
fab!(:category) { Fabricate(:category) }
|
||||
|
||||
it { is_expected.to belong_to(:category).inverse_of(:discourse_topic_voting_category_setting) }
|
||||
|
||||
describe "logs category setting changes" do
|
||||
it "logs changes when voting is enabled/disabled" do
|
||||
DiscourseTopicVoting::CategorySetting.create!(category: category)
|
||||
expect(UserHistory.count).to eq(1)
|
||||
|
||||
DiscourseTopicVoting::CategorySetting.first.destroy!
|
||||
expect(UserHistory.count).to eq(2)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue