DEV: Better categories pagination ()

Pagination is enabled only when "lazy load categories" is enabled. For
those cases when it is not, the first page should return all the
results.
This commit is contained in:
Bianca Nenciu 2024-03-28 18:19:09 +02:00 committed by GitHub
parent 5835d4e5ed
commit 3b9e9354d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View File

@ -142,13 +142,17 @@ class CategoryList
query = self.class.order_categories(query)
if @guardian.can_lazy_load_categories? && @options[:parent_category_id].blank?
page = [1, @options[:page].to_i].max
if @guardian.can_lazy_load_categories? && @options[:parent_category_id].blank?
query =
query
.where(parent_category_id: nil)
.limit(CATEGORIES_PER_PAGE)
.offset((page - 1) * CATEGORIES_PER_PAGE)
elsif page > 1
# Pagination is supported only when lazy load is enabled. If it is not,
# everything is returned on page 1.
query = query.none
end
query =

View File

@ -397,6 +397,35 @@ RSpec.describe CategoriesController do
response.parsed_body["category_list"]["categories"].map { |x| x["id"] },
).not_to include(uncategorized.id)
end
describe "with page" do
before { sign_in(admin) }
let!(:category2) { Fabricate(:category, user: admin) }
let!(:category3) { Fabricate(:category, user: admin) }
it "paginates results wihen lazy_load_categories is enabled" do
SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}"
stub_const(CategoryList, "CATEGORIES_PER_PAGE", 2) { get "/categories.json?page=1" }
expect(response.status).to eq(200)
expect(response.parsed_body["category_list"]["categories"].count).to eq(2)
stub_const(CategoryList, "CATEGORIES_PER_PAGE", 2) { get "/categories.json?page=2" }
expect(response.status).to eq(200)
expect(response.parsed_body["category_list"]["categories"].count).to eq(2)
end
it "does not paginate results when lazy_load_categories is disabled" do
stub_const(CategoryList, "CATEGORIES_PER_PAGE", 2) { get "/categories.json?page=1" }
expect(response.status).to eq(200)
expect(response.parsed_body["category_list"]["categories"].count).to eq(4)
stub_const(CategoryList, "CATEGORIES_PER_PAGE", 2) { get "/categories.json?page=2" }
expect(response.status).to eq(200)
expect(response.parsed_body["category_list"]["categories"].count).to eq(0)
end
end
end
describe "extensibility event" do