DEV: Serialize categories in topic lists (#23597)

At this moment, this feature is under a site setting named
lazy_load_categories.

In the future, categories will no longer be preloaded through site data.
This commit add information about categories in topic list and ensures
that data is used to display topic list items.

Parent categories are serialized too because they are necessary to
render {{category-link}}.
This commit is contained in:
Bianca Nenciu
2023-10-17 19:06:01 +03:00
committed by GitHub
parent 5e6c63901f
commit c95ffb98ef
6 changed files with 89 additions and 1 deletions

View File

@@ -223,6 +223,38 @@ RSpec.describe ListController do
expect(response.body).not_to include(restricted_tag.name)
end
end
context "with lazy_load_categories" do
fab!(:category) { Fabricate(:category) }
fab!(:subcategory) { Fabricate(:category, parent_category: category) }
before { topic.update!(category: subcategory) }
it "returns categories and parent categories if true" do
SiteSetting.lazy_load_categories = true
get "/latest.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].length).to eq(1)
expect(response.parsed_body["topic_list"]["topics"][0]["id"]).to eq(topic.id)
expect(response.parsed_body["topic_list"]["categories"].length).to eq(2)
expect(
response.parsed_body["topic_list"]["categories"].map { |c| c["id"] },
).to contain_exactly(category.id, subcategory.id)
end
it "does not return categories if not true" do
SiteSetting.lazy_load_categories = false
get "/latest.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].length).to eq(1)
expect(response.parsed_body["topic_list"]["topics"][0]["id"]).to eq(topic.id)
expect(response.parsed_body["topic_list"]["categories"]).to eq(nil)
end
end
end
describe "categories and X" do