Skip to content

Commit

Permalink
FIX: Don't remove custom fields when adding a new one to category ser…
Browse files Browse the repository at this point in the history
…ializer (#150)
  • Loading branch information
nattsw committed Mar 30, 2023
1 parent 552855e commit 83877c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ class Engine < ::Rails::Engine
end
end

add_to_serializer(:category, :custom_fields) do
add_to_serializer(:category, :custom_fields, false) do
return object.custom_fields if !SiteSetting.voting_enabled

object.custom_fields.merge(
enable_topic_voting:
DiscourseTopicVoting::CategorySetting.find_by(category_id: object.id).present?,
Expand Down
23 changes: 23 additions & 0 deletions spec/serializers/category_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "rails_helper"

describe CategorySerializer do
fab!(:category) { Fabricate(:category) }

it "does not return enable_topic_voting voting disabled" do
SiteSetting.voting_enabled = false

json = CategorySerializer.new(category, root: false).as_json

expect(json[:custom_fields]).to eq({})
end

it "returns enable_topic_voting when voting enabled" do
SiteSetting.voting_enabled = true

json = CategorySerializer.new(category, root: false).as_json

expect(json[:custom_fields]).to eq({ "enable_topic_voting" => false })
end
end

0 comments on commit 83877c5

Please sign in to comment.