FIX: Load complete category objects (#167)
Not all categories are preloaded when lazy_load_categories is enabled. In this case, the category objects that are loaded through /docs.json must be complete.
This commit is contained in:
parent
22107a114d
commit
e90a3193df
|
@ -1,5 +1,6 @@
|
|||
import EmberObject from "@ember/object";
|
||||
import { ajax } from "discourse/lib/ajax";
|
||||
import Site from "discourse/models/site";
|
||||
import Topic from "discourse/models/topic";
|
||||
import User from "discourse/models/user";
|
||||
import { getDocs } from "../../lib/get-docs";
|
||||
|
@ -38,6 +39,11 @@ Docs.reopenClass({
|
|||
}
|
||||
|
||||
return ajax(`/${docsPath}.json?${filters.join("&")}`).then((data) => {
|
||||
const site = Site.current();
|
||||
data.categories?.forEach((category) => site.updateCategory(category));
|
||||
data.topics.topic_list.categories?.forEach((category) =>
|
||||
site.updateCategory(category)
|
||||
);
|
||||
data.topics.topic_list.topics = data.topics.topic_list.topics.map(
|
||||
(topic) => Topic.create(topic)
|
||||
);
|
||||
|
@ -51,6 +57,10 @@ Docs.reopenClass({
|
|||
|
||||
loadMore(loadMoreUrl) {
|
||||
return ajax(loadMoreUrl).then((data) => {
|
||||
const site = Site.current();
|
||||
data.topics.topic_list.categories?.forEach((category) =>
|
||||
site.updateCategory(category)
|
||||
);
|
||||
data.topics.topic_list.topics = data.topics.topic_list.topics.map(
|
||||
(topic) => Topic.create(topic)
|
||||
);
|
||||
|
|
|
@ -210,15 +210,32 @@ module Docs
|
|||
tags_object.sort_by { |tag| [tag[:active] ? 0 : 1, -tag[:count]] }
|
||||
end
|
||||
|
||||
def create_categories_object(categories)
|
||||
categories_object = []
|
||||
def create_categories_object(category_counts)
|
||||
categories =
|
||||
Category
|
||||
.where(id: category_counts.keys)
|
||||
.includes(
|
||||
:uploaded_logo,
|
||||
:uploaded_logo_dark,
|
||||
:uploaded_background,
|
||||
:uploaded_background_dark,
|
||||
)
|
||||
.joins("LEFT JOIN topics t on t.id = categories.topic_id")
|
||||
.select("categories.*, t.slug topic_slug")
|
||||
|
||||
categories.each do |category|
|
||||
active = @filters[:category].include?(category[0].to_s) if @filters[:category]
|
||||
categories_object << { id: category[0], count: category[1], active: active || false }
|
||||
end
|
||||
Category.preload_user_fields!(@guardian, categories)
|
||||
|
||||
categories_object.sort_by { |category| [category[:active] ? 0 : 1, -category[:count]] }
|
||||
categories
|
||||
.map do |category|
|
||||
count = category_counts[category.id]
|
||||
active = @filters[:category] && @filters[:category].include?(category.id.to_s)
|
||||
|
||||
BasicCategorySerializer
|
||||
.new(category, scope: @guardian, root: false)
|
||||
.as_json
|
||||
.merge(count:, active:)
|
||||
end
|
||||
.sort_by { |category| [category[:active] ? 0 : 1, -category[:count]] }
|
||||
end
|
||||
|
||||
def load_more_url
|
||||
|
|
|
@ -174,8 +174,8 @@ describe Docs::DocsController do
|
|||
topics = json["topics"]["topic_list"]["topics"]
|
||||
|
||||
expect(categories.size).to eq(2)
|
||||
expect(categories[0]).to eq({ "active" => true, "count" => 1, "id" => category2.id })
|
||||
expect(categories[1]).to eq({ "active" => false, "count" => 2, "id" => category.id })
|
||||
expect(categories[0]).to include({ "active" => true, "count" => 1, "id" => category2.id })
|
||||
expect(categories[1]).to include({ "active" => false, "count" => 2, "id" => category.id })
|
||||
expect(topics.size).to eq(1)
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue