From 0fa41a8b388a58a68c3bf9a2b230b0e0ab3f856b Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Fri, 15 Mar 2019 17:25:17 +0530 Subject: [PATCH] PERF: Remove N+1 queries on site serializer. --- plugin.rb | 16 ++++++++++++++-- spec/models/site_spec.rb | 4 ++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/plugin.rb b/plugin.rb index e7b9df4..24bb46c 100644 --- a/plugin.rb +++ b/plugin.rb @@ -329,11 +329,23 @@ SQL attributes :custom_fields def custom_fields - object.custom_fields.slice("enable_accepted_answers") + object.custom_fields end def include_custom_fields? - custom_fields.present? + 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 diff --git a/spec/models/site_spec.rb b/spec/models/site_spec.rb index 09cfac3..2e4e27b 100644 --- a/spec/models/site_spec.rb +++ b/spec/models/site_spec.rb @@ -5,6 +5,10 @@ describe Site do let(:category) { Fabricate(:category) } let(:guardian) { Guardian.new } + before do + SiteSetting.show_filter_by_solved_status = true + end + it "includes `enable_accepted_answers` custom field for categories" do category.custom_fields["enable_accepted_answers"] = true category.save_custom_fields