From 5e410dc5e0eff133d0b2e396d022fa1bf2ef85e4 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 18 Mar 2019 15:25:45 +0800 Subject: [PATCH] FEATURE: Ability to exclude category from search results. (#7194) This commit also adds `Category#search_priority` which sets the ground work to enable prioritizing of posts for certain categories when searching. --- .../category_search_priorities.js.es6.erb | 1 + .../components/edit-category-settings.js.es6 | 15 ++++ .../discourse/models/category.js.es6 | 3 +- .../components/edit-category-settings.hbs | 11 +++ app/controllers/categories_controller.rb | 1 + app/models/category.rb | 7 +- app/models/concerns/searchable.rb | 5 ++ app/serializers/category_serializer.rb | 3 +- config/locales/client.en.yml | 5 ++ ...82018_add_search_priority_to_categories.rb | 6 ++ lib/search.rb | 48 +++++++----- spec/components/search_spec.rb | 74 ++++++++++++++++--- spec/models/category_spec.rb | 13 ++++ spec/requests/categories_controller_spec.rb | 2 + .../acceptance/category-edit-test.js.es6 | 21 ++---- 15 files changed, 170 insertions(+), 45 deletions(-) create mode 100644 app/assets/javascripts/discourse/components/concerns/category_search_priorities.js.es6.erb create mode 100644 db/migrate/20190314082018_add_search_priority_to_categories.rb diff --git a/app/assets/javascripts/discourse/components/concerns/category_search_priorities.js.es6.erb b/app/assets/javascripts/discourse/components/concerns/category_search_priorities.js.es6.erb new file mode 100644 index 00000000000..643b1777f96 --- /dev/null +++ b/app/assets/javascripts/discourse/components/concerns/category_search_priorities.js.es6.erb @@ -0,0 +1 @@ +export const searchPriorities = <%= Searchable::PRIORITIES.to_json %>; diff --git a/app/assets/javascripts/discourse/components/edit-category-settings.js.es6 b/app/assets/javascripts/discourse/components/edit-category-settings.js.es6 index 712aa67be17..82af15b8f0d 100644 --- a/app/assets/javascripts/discourse/components/edit-category-settings.js.es6 +++ b/app/assets/javascripts/discourse/components/edit-category-settings.js.es6 @@ -1,6 +1,7 @@ import { setting } from "discourse/lib/computed"; import { buildCategoryPanel } from "discourse/components/edit-category-panel"; import computed from "ember-addons/ember-computed-decorators"; +import { searchPriorities } from "discourse/components/concerns/category_search_priorities"; const categorySortCriteria = []; export function addCategorySortCriteria(criteria) { @@ -57,6 +58,20 @@ export default buildCategoryPanel("settings", { ); }, + @computed + searchPrioritiesOptions() { + const options = []; + + for (const [name, value] of Object.entries(searchPriorities)) { + options.push({ + name: I18n.t(`category.search_priority.options.${name}`), + value: value + }); + } + + return options.sort((a, b) => a.value <= b.value); + }, + @computed availableSorts() { return [ diff --git a/app/assets/javascripts/discourse/models/category.js.es6 b/app/assets/javascripts/discourse/models/category.js.es6 index 2e6928abfe4..d31fcb72b45 100644 --- a/app/assets/javascripts/discourse/models/category.js.es6 +++ b/app/assets/javascripts/discourse/models/category.js.es6 @@ -129,7 +129,8 @@ const Category = RestModel.extend({ minimum_required_tags: this.get("minimum_required_tags"), navigate_to_first_post_after_read: this.get( "navigate_to_first_post_after_read" - ) + ), + search_priority: this.get("search_priority") }, type: id ? "PUT" : "POST" }); diff --git a/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs b/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs index f9628a3fa46..1aab6209b31 100644 --- a/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs +++ b/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs @@ -43,6 +43,17 @@ +
+ + + {{combo-box valueAttribute="value" + id="category-search-priority" + content=searchPrioritiesOptions + value=category.search_priority}} +
+ {{#if isParentCategory}}