PERF: Use 1 query to load group and tag notification default.

This commit is contained in:
Guo Xiang Tan 2020-09-09 14:57:07 +08:00 committed by Alan Guo Xiang Tan
parent 40d5739a80
commit 5ef9d4d537
1 changed files with 14 additions and 2 deletions

View File

@ -134,11 +134,23 @@ class BasicGroupSerializer < ApplicationSerializer
[:watching, :regular, :tracking, :watching_first_post, :muted].each do |level| [:watching, :regular, :tracking, :watching_first_post, :muted].each do |level|
define_method("#{level}_category_ids") do define_method("#{level}_category_ids") do
GroupCategoryNotificationDefault.lookup(object, level).pluck(:category_id) @group_category_notification_defaults ||= begin
GroupCategoryNotificationDefault.where(group: object).pluck(:category_id)
end
@group_category_notification_defaults.filter do |default|
default.notification_level == NotificationLevels.all[level]
end
end end
define_method("#{level}_tags") do define_method("#{level}_tags") do
GroupTagNotificationDefault.lookup(object, level).joins(:tag).pluck('tags.name') @group_tag_notification_defaults ||= begin
GroupTagNotificationDefault.where(group: object).joins(:tag).pluck('tags.name')
end
@group_tag_notification_defaults.filter do |default|
default.notification_level == NotificationLevels.all[level]
end
end end
end end