DEV: Use core method to preload category custom fields for site serializer

DEV: Use core method to preload category custom fields for site serializer

diff --git a/plugin.rb b/plugin.rb
index 2807697..1f58739 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -324,31 +324,6 @@ SQL
     end
   end
 
-  require_dependency 'basic_category_serializer'
-  class ::BasicCategorySerializer
-    attributes :custom_fields
-
-    def custom_fields
-      object.custom_fields
-    end
-
-    def include_custom_fields?
-      object.custom_field_preloaded?("enable_accepted_answers") && SiteSetting.show_filter_by_solved_status && custom_fields.present?
-    end
-  end
-
-  require_dependency 'site'
-  class ::Site
-    alias_method :discourse_categories, :categories
-
-    def categories
-      @categories ||= begin
-        Category.preload_custom_fields(discourse_categories, ["enable_accepted_answers"]) if SiteSetting.show_filter_by_solved_status
-        discourse_categories
-      end
-    end
-  end
-
   require_dependency 'topic_view_serializer'
   class ::TopicViewSerializer
     attributes :accepted_answer
@@ -529,6 +504,7 @@ SQL
   end
 
   TopicList.preloaded_custom_fields << "accepted_answer_post_id" if TopicList.respond_to? :preloaded_custom_fields
+  Site.preloaded_category_custom_fields << "enable_accepted_answers" if Site.respond_to? :preloaded_category_custom_fields
 
   if CategoryList.respond_to?(:preloaded_topic_custom_fields)
     CategoryList.preloaded_topic_custom_fields << "accepted_answer_post_id"
diff --git a/spec/serializers/basic_category_serializer_spec.rb b/spec/serializers/basic_category_serializer_spec.rb
deleted file mode 100644
index c707a9a..0000000
--- a/spec/serializers/basic_category_serializer_spec.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-require 'rails_helper'
-
-describe BasicCategorySerializer do
-  let(:category) { Fabricate(:category) }
-  let(:guardian) { Guardian.new }
-
-  before do
-    SiteSetting.show_filter_by_solved_status = true
-    category.custom_fields["enable_accepted_answers"] = true
-    category.save_custom_fields
-  end
-
-  it "should include custom fields only if its preloaded" do
-    json = described_class.new(category, scope: guardian, root: false).as_json
-    expect(json.to_s).not_to include("custom_fields")
-
-    category.expects(:custom_field_preloaded?).returns(true)
-    json = described_class.new(category, scope: guardian, root: false).as_json
-    expect(json.to_s).to include("custom_fields")
-  end  
-end

GitHub sha: 8b8512e7

1 Like

Previously, we wouldn’t preload the custom fields if the SiteSetting isn’t enabled. Are we now doing an extra query even if the plugin isn’t enabled?