searchable: factor out metatag value parser.

Factor out the code that parses metatag values (e.g. `score:>5`) and
search URL params (e.g. `search[score]=>5`) into a RangeParser class.

Also fix a bug where, if the `search[order]=custom` param was used
without a `search[id]` param, an exception would be raised. Fix another
bug where if an invalid `search[id]` was provided, then the custom order
would be ignored and the search would be returned with the default order
instead. Now if you use `search[order]=custom` without a valid
`search[id]` param, the search will return no results.
This commit is contained in:
evazion
2022-09-26 22:46:46 -05:00
parent 65dbd89e25
commit 116c1f1af8
4 changed files with 201 additions and 148 deletions

View File

@@ -310,6 +310,30 @@ class ApplicationControllerTest < ActionDispatch::IntegrationTest
assert_equal(tags.first.id, response.parsed_body.first.fetch("id"))
end
should "support ordering by search[order]=custom" do
tags = create_list(:tag, 2, post_count: 42)
get tags_path, params: { search: { id: "#{tags[0].id},#{tags[1].id}", order: "custom" } }, as: :json
assert_response :success
assert_equal(tags.pluck(:id), response.parsed_body.pluck("id"))
end
should "return nothing if the search[order]=custom param isn't accompanied by search[id]" do
tags = create_list(:tag, 2, post_count: 42)
get tags_path, params: { search: { order: "custom" } }, as: :json
assert_response :success
assert_equal(0, response.parsed_body.size)
end
should "return nothing if the search[order]=custom param isn't accompanied by a valid search[id]" do
tags = create_list(:tag, 2, post_count: 42)
get tags_path, params: { search: { id: ">1", order: "custom" } }, as: :json
assert_response :success
assert_equal(0, response.parsed_body.size)
end
should "support the expiry parameter" do
get posts_path, as: :json, params: { expiry: "1" }