mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Push category hashtag slug match to top (#19174)
When searching for categories it is possible for a child category to have a slug that matches the term exactly, but will not be found by .lookup since we don't return these categories unless the ref matches parent:child. Introduces a search_sort method to each hashtag data source so they can provide their custom sort logic of results, in category's case putting all matching slugs to the top regardless of parent/child relationship then sorting by text.
This commit is contained in:
@@ -43,6 +43,10 @@ RSpec.describe HashtagAutocompleteService do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.search_sort(search_results, _)
|
||||
search_results.sort_by { |item| item.text.downcase }
|
||||
end
|
||||
end
|
||||
|
||||
describe ".contexts_with_ordered_types" do
|
||||
@@ -166,6 +170,37 @@ RSpec.describe HashtagAutocompleteService do
|
||||
)
|
||||
end
|
||||
|
||||
it "orders categories by exact match on slug (ignoring parent/child distinction) then name, and then name for everything else" do
|
||||
category2 = Fabricate(:category, name: "Book Library", slug: "book-library")
|
||||
Fabricate(:category, name: "Horror", slug: "book", parent_category: category2)
|
||||
Fabricate(:category, name: "Romance", slug: "romance-books")
|
||||
Fabricate(:category, name: "Abstract Philosophy", slug: "abstract-philosophy-books")
|
||||
category6 = Fabricate(:category, name: "Book Reviews", slug: "book-reviews")
|
||||
Fabricate(:category, name: "Good Books", slug: "book", parent_category: category6)
|
||||
expect(subject.search("book", %w[category]).map(&:ref)).to eq(
|
||||
%w[
|
||||
book-reviews:book
|
||||
book-library:book
|
||||
abstract-philosophy-books
|
||||
book-club
|
||||
book-library
|
||||
book-reviews
|
||||
romance-books
|
||||
],
|
||||
)
|
||||
expect(subject.search("book", %w[category]).map(&:text)).to eq(
|
||||
[
|
||||
"Good Books",
|
||||
"Horror",
|
||||
"Abstract Philosophy",
|
||||
"Book Club",
|
||||
"Book Library",
|
||||
"Book Reviews",
|
||||
"Romance",
|
||||
],
|
||||
)
|
||||
end
|
||||
|
||||
context "when multiple tags and categories are returned" do
|
||||
fab!(:category2) { Fabricate(:category, name: "Book Zone", slug: "book-zone") }
|
||||
fab!(:category3) { Fabricate(:category, name: "Book Dome", slug: "book-dome") }
|
||||
|
||||
Reference in New Issue
Block a user