search: support backwards ranges.

Fix things like age:1y..2y or score:5..0 returning no results.
This commit is contained in:
evazion
2020-04-20 02:05:10 -05:00
parent 172095730c
commit 25159bb21c
2 changed files with 4 additions and 3 deletions

View File

@@ -1146,7 +1146,8 @@ class PostQueryBuilder
def parse_range(string, type = :integer)
range = case string
when /\A(.+?)\.\.(.+)/
[:between, (parse_cast($1, type)..parse_cast($2, type))]
lo, hi = [parse_cast($1, type), parse_cast($2, type)].sort
[:between, (lo..hi)]
when /\A<=(.+)/, /\A\.\.(.+)/
[:lteq, parse_cast($1, type)]
when /\A<(.+)/
@@ -1180,8 +1181,6 @@ class PostQueryBuilder
def reverse_range(range)
case range
in [:between, range]
[:between, (range.end..range.begin)]
in [:lteq, value]
[:gteq, value]
in [:lt, value]

View File

@@ -142,6 +142,7 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
assert_tag_match([posts[1], posts[0]], "id:<=#{posts[1].id}")
assert_tag_match([posts[2], posts[0]], "id:#{posts[0].id},#{posts[2].id}")
assert_tag_match(posts.reverse, "id:#{posts[0].id}..#{posts[2].id}")
assert_tag_match(posts.reverse, "id:#{posts[2].id}..#{posts[0].id}")
assert_tag_match([], "id:#{posts[0].id} id:#{posts[2].id}")
assert_tag_match([posts[1]], "id:>#{posts[0].id} id:<#{posts[2].id}")
@@ -420,6 +421,7 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
assert_tag_match([post], "age:>0s")
assert_tag_match([post], "age:>=0s")
assert_tag_match([post], "age:0s..1m")
assert_tag_match([post], "age:1m..0s")
assert_tag_match([], "age:>1y")
assert_tag_match([], "age:>=1y")