FIX: Set has_children correctly in Category.preload_user_fields! (#26327)

This commit is contained in:
Daniel Waterworth 2024-03-22 12:41:28 -05:00 committed by GitHub
parent 4a7e69d8ee
commit d52abe2324
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View File

@ -241,7 +241,7 @@ class Category < ActiveRecord::Base
# Categories with children # Categories with children
with_children = with_children =
Category Category
.secured(@guardian) .secured(guardian)
.where(parent_category_id: category_ids) .where(parent_category_id: category_ids)
.pluck(:parent_category_id) .pluck(:parent_category_id)
.to_set .to_set

View File

@ -1140,6 +1140,26 @@ RSpec.describe CategoriesController do
expect(category["permission"]).to eq(CategoryGroup.permission_types[:full]) expect(category["permission"]).to eq(CategoryGroup.permission_types[:full])
expect(category["has_children"]).to eq(true) expect(category["has_children"]).to eq(true)
end end
context "with a read restricted child category" do
before_all { subcategory.update!(read_restricted: true) }
it "indicates to an admin that the category has a child" do
sign_in(admin)
get "/categories/find.json", params: { ids: [category.id] }
category = response.parsed_body["categories"].first
expect(category["has_children"]).to eq(true)
end
it "indicates to a normal user that the category has no child" do
sign_in(user)
get "/categories/find.json", params: { ids: [category.id] }
category = response.parsed_body["categories"].first
expect(category["has_children"]).to eq(false)
end
end
end end
describe "#search" do describe "#search" do