Skip to content

A platform for community discussion. Free, open, simple.

FIX: subcategory filter limits results (#15655)

Krzysztof Kotlarek51a31f783502
diff --git a/app/assets/javascripts/discourse/app/models/category.js b/app/assets/javascripts/discourse/app/models/category.js
index 1636d8ddb50..fca4880d7d5 100644
--- a/app/assets/javascripts/discourse/app/models/category.js
+++ b/app/assets/javascripts/discourse/app/models/category.js
@@ -542,6 +542,7 @@ Category.reopenClass({
search(term, opts) {
let limit = 5;
+ let parentCategoryId;
if (opts) {
if (opts.limit === 0) {
@@ -549,6 +550,9 @@ Category.reopenClass({
} else if (opts.limit) {
limit = opts.limit;
}
+ if (opts.parentCategoryId) {
+ parentCategoryId = opts.parentCategoryId;
+ }
}
const emptyTerm = term === "";
@@ -569,13 +573,21 @@ Category.reopenClass({
return data.length === limit;
};
+ const validCategoryParent = (category) => {
+ return (
+ !parentCategoryId ||
+ category.get("parent_category_id") === parentCategoryId
+ );
+ };
+
for (i = 0; i < length && !done(); i++) {
const category = categories[i];
if (
- (emptyTerm && !category.get("parent_category_id")) ||
- (!emptyTerm &&
- (category.get("name").toLowerCase().indexOf(term) === 0 ||
- category.get("slug").toLowerCase().indexOf(slugTerm) === 0))
+ ((emptyTerm && !category.get("parent_category_id")) ||
+ (!emptyTerm &&
+ (category.get("name").toLowerCase().indexOf(term) === 0 ||
+ category.get("slug").toLowerCase().indexOf(slugTerm) === 0))) &&
+ validCategoryParent(category)
) {
data.push(category);
}
@@ -586,9 +598,10 @@ Category.reopenClass({
const category = categories[i];
if (
- !emptyTerm &&
- (category.get("name").toLowerCase().indexOf(term) > 0 ||
- category.get("slug").toLowerCase().indexOf(slugTerm) > 0)
+ ((!emptyTerm &&
+ category.get("name").toLowerCase().indexOf(term) > 0) ||
+ category.get("slug").toLowerCase().indexOf(slugTerm) > 0) &&
+ validCategoryParent(category)
) {
if (data.indexOf(category) === -1) {
data.push(category);
diff --git a/app/assets/javascripts/discourse/tests/unit/models/category-test.js b/app/assets/javascripts/discourse/tests/unit/models/category-test.js
index 18c2f7cee5d..27c4193c801 100644
--- a/app/assets/javascripts/discourse/tests/unit/models/category-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/models/category-test.js
@@ -276,9 +276,17 @@ module("Unit | Model | category", function () {
id: 2,
name: "middle term",
slug: "another-different-slug",
+ }),
+ subcategory = store.createRecord("category", {
+ id: 3,
+ name: "middle term",
+ slug: "another-different-slug2",
+ parent_category_id: 2,
});
- sinon.stub(Category, "listByActivity").returns([category1, category2]);
+ sinon
+ .stub(Category, "listByActivity")
+ .returns([category1, category2, subcategory]);
assert.deepEqual(
Category.search("term", { limit: 0 }),
@@ -292,24 +300,30 @@ module("Unit | Model | category", function () {
);
assert.deepEqual(
Category.search("term"),
- [category1, category2],
+ [category1, category2, subcategory],
"orders by activity"
);
category2.set("name", "TeRm start");
assert.deepEqual(
Category.search("tErM"),
- [category2, category1],
+ [category2, category1, subcategory],
"ignores case of category name and search term"
);
category2.set("name", "term start");
assert.deepEqual(
Category.search("term"),
- [category2, category1],
+ [category2, category1, subcategory],
"orders matching begin with and then contains"
);
+ assert.deepEqual(
+ Category.search("term", { parentCategoryId: 2 }),
+ [subcategory],
+ "search only subcategories belonging to specific parent category"
+ );
+
sinon.restore();
const child_category1 = store.createRecord("category", {
diff --git a/app/assets/javascripts/select-kit/addon/components/category-drop.js b/app/assets/javascripts/select-kit/addon/components/category-drop.js
index 5b19218fd67..925df37ed45 100644
--- a/app/assets/javascripts/select-kit/addon/components/category-drop.js
+++ b/app/assets/javascripts/select-kit/addon/components/category-drop.js
@@ -131,7 +131,10 @@ export default ComboBoxComponent.extend({
search(filter) {
if (filter) {
- let results = Category.search(filter);
+ let opts = {
+ parentCategoryId: this.options.parentCategory?.id,
+ };
+ let results = Category.search(filter, opts);
results = this._filterUncategorized(results).sort((a, b) => {
if (a.parent_category_id && !b.parent_category_id) {
return 1;