DEV: Merge two similar category serializers (#26653)

CategoryBadgeSerializer and TopicCategorySerializer are too similar and
are used in similar contexts. This commit merges the two into a single
CategoryBadgeSerializer.
This commit is contained in:
Bianca Nenciu 2024-04-17 18:52:34 +03:00 committed by GitHub
parent 9638ce17fa
commit 6281f5d768
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 35 deletions

View File

@ -1,5 +1,25 @@
# frozen_string_literal: true
class CategoryBadgeSerializer < ApplicationSerializer
attributes :id, :name, :color, :slug, :parent_category_id
attributes :id, :name, :slug, :color, :text_color, :read_restricted, :parent_category_id
def include_parent_category_id?
parent_category_id.present?
end
def name
if object.uncategorized?
I18n.t("uncategorized_category_name", locale: SiteSetting.default_locale)
else
object.name
end
end
def description_text
if object.uncategorized?
I18n.t("category.uncategorized_description", locale: SiteSetting.default_locale)
else
object.description_text
end
end
end

View File

@ -1,32 +0,0 @@
# frozen_string_literal: true
class TopicCategorySerializer < ApplicationSerializer
attributes :id,
:name,
:color,
:text_color,
:slug,
:description_text,
:read_restricted,
:parent_category_id
def include_parent_category_id?
parent_category_id
end
def name
if object.uncategorized?
I18n.t("uncategorized_category_name", locale: SiteSetting.default_locale)
else
object.name
end
end
def description_text
if object.uncategorized?
I18n.t("category.uncategorized_description", locale: SiteSetting.default_locale)
else
object.description_text
end
end
end

View File

@ -12,7 +12,7 @@ class TopicListSerializer < ApplicationSerializer
has_many :topics, serializer: TopicListItemSerializer, embed: :objects
has_many :shared_drafts, serializer: TopicListItemSerializer, embed: :objects
has_many :tags, serializer: TagSerializer, embed: :objects
has_many :categories, serializer: TopicCategorySerializer, embed: :objects
has_many :categories, serializer: CategoryBadgeSerializer, embed: :objects
def can_create_topic
scope.can_create?(Topic)

View File

@ -83,7 +83,7 @@ class TopicViewSerializer < ApplicationSerializer
has_one :details, serializer: TopicViewDetailsSerializer, root: false, embed: :objects
has_many :pending_posts, serializer: TopicPendingPostSerializer, root: false, embed: :objects
has_many :categories, serializer: TopicCategorySerializer, embed: :objects
has_many :categories, serializer: CategoryBadgeSerializer, embed: :objects
has_one :published_page, embed: :objects