mirror of
https://github.com/discourse/discourse.git
synced 2024-11-30 15:15:50 +08:00
fe676f334a
* FEATURE: Return subcategories on categories endpoint When using the API subcategories will now be returned nested inside of each category response under the `subcategory_list` param. We already return all the subcategory ids under the `subcategory_ids` param, but you then would have to make multiple separate API calls to fetch each of those subcategories. This way you can get **ALL** of the categories along with their subcategories in a single API response. The UI will not be affected by this change because you need to pass in the `include_subcategories=true` param in order for subcategories to be returned. In a follow up PR I'll add the API scoping for fetching categories so that a readonly API key can be used for the `/categories.json` endpoint. This endpoint should be used instead of the `/site.json` endpoint for fetching a sites categories and subcategories. * Update PR based on feedback - Have spec check for specific subcategory - Move comparison check out of loop - Only populate subcategory list if option present - Remove empty array initialization - Update api spec to allow null response * More PR updates based on feedback - Use a category serializer for the subcategory_list - Don't include the subcategory_list param if empty - For the spec check for the subcategory by id - Fix spec to account for param not present when empty
66 lines
1.4 KiB
Ruby
66 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class CategoryDetailedSerializer < BasicCategorySerializer
|
|
|
|
attributes :topic_count,
|
|
:post_count,
|
|
:topics_day,
|
|
:topics_week,
|
|
:topics_month,
|
|
:topics_year,
|
|
:topics_all_time,
|
|
:is_uncategorized,
|
|
:subcategory_ids
|
|
|
|
has_many :displayable_topics, serializer: ListableTopicSerializer, embed: :objects, key: :topics
|
|
|
|
has_many :subcategory_list, serializer: CategoryDetailedSerializer, embed: :objects, key: :subcategory_list
|
|
|
|
def include_displayable_topics?
|
|
displayable_topics.present?
|
|
end
|
|
|
|
def include_subcategory_list?
|
|
subcategory_list.present?
|
|
end
|
|
|
|
def is_uncategorized
|
|
object.id == SiteSetting.uncategorized_category_id
|
|
end
|
|
|
|
def include_is_uncategorized?
|
|
is_uncategorized
|
|
end
|
|
|
|
def topics_day
|
|
count_with_subcategories(:topics_day)
|
|
end
|
|
|
|
def topics_week
|
|
count_with_subcategories(:topics_week)
|
|
end
|
|
|
|
def topics_month
|
|
count_with_subcategories(:topics_month)
|
|
end
|
|
|
|
def topics_year
|
|
count_with_subcategories(:topics_year)
|
|
end
|
|
|
|
def topics_all_time
|
|
count_with_subcategories(:topic_count)
|
|
end
|
|
|
|
def count_with_subcategories(method)
|
|
count = object.public_send(method) || 0
|
|
|
|
object.subcategories.each do |category|
|
|
count += (category.public_send(method) || 0)
|
|
end
|
|
|
|
count
|
|
end
|
|
|
|
end
|