forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Allow categories to be prioritized/deprioritized in search. (d…
- Loading branch information
Showing
10 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
lib/validators/category_search_priority_weights_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class CategorySearchPriorityWeightsValidator | ||
def initialize(opts = {}) | ||
@name = opts[:name].to_s | ||
end | ||
|
||
def valid_value?(val) | ||
val = val.to_f | ||
|
||
case @name | ||
when "category_search_priority_very_low_weight" | ||
val < SiteSetting.category_search_priority_low_weight | ||
when "category_search_priority_low_weight" | ||
val < 1 && val > SiteSetting.category_search_priority_very_low_weight | ||
when "category_search_priority_high_weight" | ||
val > 1 && val < SiteSetting.category_search_priority_very_high_weight | ||
when "category_search_priority_very_high_weight" | ||
val > SiteSetting.category_search_priority_high_weight | ||
end | ||
end | ||
|
||
def error_message | ||
key = @name[/category_search_priority_(\w+)_weight/, 1] | ||
I18n.t("site_settings.errors.category_search_priority.#{key}_weight_invalid") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
spec/components/validators/category_searrch_priority_weights_validator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'rails_helper' | ||
require 'validators/category_search_priority_weights_validator' | ||
|
||
RSpec.describe CategorySearchPriorityWeightsValidator do | ||
it "should validate the results correctly" do | ||
expect do | ||
SiteSetting.category_search_priority_very_low_weight = 0.9 | ||
end.to raise_error(Discourse::InvalidParameters) | ||
|
||
[1, 0].each do |value| | ||
expect do | ||
SiteSetting.category_search_priority_low_weight = value | ||
end.to raise_error(Discourse::InvalidParameters) | ||
end | ||
|
||
['0.2', 10].each do |value| | ||
expect do | ||
SiteSetting.category_search_priority_high_weight = value | ||
end.to raise_error(Discourse::InvalidParameters) | ||
end | ||
|
||
expect do | ||
SiteSetting.category_search_priority_very_high_weight = 1.1 | ||
end.to raise_error(Discourse::InvalidParameters) | ||
end | ||
end |