FIX: add category hashtags support for sub-sub categories.

Hashtags will include last two levels only (ex: "parent:child").
This commit is contained in:
Vinoth Kannan 2020-04-06 20:43:38 +05:30
parent a947b7b839
commit fd39c85c1a
4 changed files with 21 additions and 8 deletions

View File

@ -76,7 +76,7 @@ export function search(term, siteSettings) {
var numOfCategories = categories.length;
categories = categories.map(category => {
return { model: category, text: Category.slugFor(category, SEPARATOR) };
return { model: category, text: Category.slugFor(category, SEPARATOR, 2) };
});
if (numOfCategories !== limit && siteSettings.tagging_enabled) {

View File

@ -264,14 +264,15 @@ Category.reopenClass({
return _uncategorized;
},
slugFor(category, separator = "/") {
slugFor(category, separator = "/", depth = 3) {
if (!category) return "";
const parentCategory = get(category, "parentCategory");
let result = "";
if (parentCategory) {
result = Category.slugFor(parentCategory) + separator;
if (parentCategory && depth > 1) {
result =
Category.slugFor(parentCategory, separator, depth - 1) + separator;
}
const id = get(category, "id"),

View File

@ -9,17 +9,17 @@ module CategoryHashtag
def query_from_hashtag_slug(category_slug)
parent_slug, child_slug = category_slug.split(SEPARATOR, 2)
category = Category.where(slug: parent_slug, parent_category_id: nil)
categories = Category.where(slug: parent_slug)
if child_slug
Category.where(slug: child_slug, parent_category_id: category.select(:id)).first
Category.where(slug: child_slug, parent_category_id: categories.select(:id)).first
else
category.first
categories.where(parent_category_id: nil).first
end
end
end
def hashtag_slug
full_slug(SEPARATOR)
full_slug.split("-").last(2).join(SEPARATOR)
end
end

View File

@ -36,5 +36,17 @@ describe CategoryHashtag do
expect(Category.query_from_hashtag_slug("apple")).to eq(nil)
expect(Category.query_from_hashtag_slug("apple#{CategoryHashtag::SEPARATOR}orange")).to eq(nil)
end
context "multi-level categories" do
before do
SiteSetting.max_category_nesting = 3
end
it "should return the right result for a grand child category slug" do
category = Fabricate(:category, parent_category: child_category)
expect(Category.query_from_hashtag_slug("#{child_category.slug}#{CategoryHashtag::SEPARATOR}#{category.slug}"))
.to eq(category)
end
end
end
end