discourse/app/serializers/basic_category_serializer.rb
Bianca Nenciu dcd81d56c0
FIX: category selectors for lazy loaded categories (#24533)
A lot of work has been put in the select kits used for selecting
categories: CategorySelector, CategoryChooser, CategoryDrop, however
they still do not work as expected when these selectors already have
values set, because the category were still looked up in the list of
categories stored on the client-side Categrories.list().

This PR fixes that by looking up the categories when the selector is
initialized. This required altering the /categories/find.json endpoint
to accept a list of IDs that need to be looked up. The API is called
using Category.asyncFindByIds on the client-side.

CategorySelector was also updated to receive a list of category IDs as
attribute, instead of the list of categories, because the list of
categories may have not been loaded.

During this development, I noticed that SiteCategorySerializer did not
serializer all fields (such as permission and notification_level)
which are not a property of category, but a property of the relationship
between users and categories. To make this more efficient, the
preload_user_fields! method was implemented that can be used to
preload these attributes for a user and a list of categories.
2023-12-08 12:01:08 +02:00

96 lines
2.3 KiB
Ruby

# frozen_string_literal: true
class BasicCategorySerializer < ApplicationSerializer
attributes :id,
:name,
:color,
:text_color,
:slug,
:topic_count,
:post_count,
:position,
:description,
:description_text,
:description_excerpt,
:topic_url,
:read_restricted,
:permission,
:parent_category_id,
:notification_level,
:can_edit,
:topic_template,
:has_children,
:sort_order,
:sort_ascending,
:show_subcategory_list,
:num_featured_topics,
:default_view,
:subcategory_list_style,
:default_top_period,
:default_list_filter,
:minimum_required_tags,
:navigate_to_first_post_after_read,
:custom_fields
has_one :uploaded_logo, embed: :object, serializer: CategoryUploadSerializer
has_one :uploaded_logo_dark, embed: :object, serializer: CategoryUploadSerializer
has_one :uploaded_background, embed: :object, serializer: CategoryUploadSerializer
has_one :uploaded_background_dark, embed: :object, serializer: CategoryUploadSerializer
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
def description
if object.uncategorized?
I18n.t("category.uncategorized_description", locale: SiteSetting.default_locale)
else
object.description
end
end
def description_excerpt
if object.uncategorized?
I18n.t("category.uncategorized_description", locale: SiteSetting.default_locale)
else
object.description_excerpt
end
end
def can_edit
true
end
def include_can_edit?
scope && scope.can_edit?(object)
end
def notification_level
object.notification_level
end
def custom_fields
object.preloaded_custom_fields
end
def include_custom_fields?
custom_fields.present?
end
end