From 0e48f1aabe7f1be7de249f9f48f847d095e2f06a Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Thu, 11 Jul 2024 19:59:57 +0300 Subject: [PATCH] FIX: Include permissions in the tag serializer (#27849) The 'permissions' field is used by the composer and the category chooser to render the category. --- app/models/tag.rb | 12 ++++++++++++ app/serializers/detailed_tag_serializer.rb | 12 ++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index 8637eac2be9..6a589388168 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -243,6 +243,18 @@ class Tag < ActiveRecord::Base end end + def all_category_ids + @all_category_ids ||= + categories.pluck(:id) + + tag_groups.includes(:categories).flat_map { |tg| tg.categories.map(&:id) } + end + + def all_categories(guardian) + categories = Category.secured(guardian).where(id: all_category_ids) + Category.preload_user_fields!(guardian, categories) + categories + end + %i[tag_created tag_updated tag_destroyed].each do |event| define_method("trigger_#{event}_event") do DiscourseEvent.trigger(event, self) diff --git a/app/serializers/detailed_tag_serializer.rb b/app/serializers/detailed_tag_serializer.rb index 7fd7a3943b0..1870b4d4dbf 100644 --- a/app/serializers/detailed_tag_serializer.rb +++ b/app/serializers/detailed_tag_serializer.rb @@ -10,11 +10,11 @@ class DetailedTagSerializer < TagSerializer end def categories - Category.secured(scope).where(id: category_ids) + object.all_categories(scope) end def category_restricted - !category_ids.empty? + object.all_category_ids.present? end def include_tag_group_names? @@ -24,12 +24,4 @@ class DetailedTagSerializer < TagSerializer def tag_group_names object.tag_groups.map(&:name) end - - private - - def category_ids - @_category_ids ||= - object.categories.pluck(:id) + - object.tag_groups.includes(:categories).map { |tg| tg.categories.map(&:id) }.flatten - end end