PERF: Remove N+1 queries on site serializer.

This commit is contained in:
Vinoth Kannan 2019-03-15 17:25:17 +05:30
parent effb7e168b
commit 0fa41a8b38
2 changed files with 18 additions and 2 deletions

View File

@ -329,11 +329,23 @@ SQL
attributes :custom_fields attributes :custom_fields
def custom_fields def custom_fields
object.custom_fields.slice("enable_accepted_answers") object.custom_fields
end end
def include_custom_fields? 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
end end

View File

@ -5,6 +5,10 @@ describe Site do
let(:category) { Fabricate(:category) } let(:category) { Fabricate(:category) }
let(:guardian) { Guardian.new } let(:guardian) { Guardian.new }
before do
SiteSetting.show_filter_by_solved_status = true
end
it "includes `enable_accepted_answers` custom field for categories" do it "includes `enable_accepted_answers` custom field for categories" do
category.custom_fields["enable_accepted_answers"] = true category.custom_fields["enable_accepted_answers"] = true
category.save_custom_fields category.save_custom_fields