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