Commit c9265a220bf4467feef1c0e7b2b76bb226f1a539
Committed by
GitHub
1 parent
75250b23
Exists in
master
and in
2 other branches
Support beginless ranges in where queries (#1527)
Showing
2 changed files
with
17 additions
and
6 deletions
Show diff stats
lib/searchkick/query.rb
... | ... | @@ -904,12 +904,7 @@ module Searchkick |
904 | 904 | else |
905 | 905 | # expand ranges |
906 | 906 | if value.is_a?(Range) |
907 | - # infinite? added in Ruby 2.4 | |
908 | - if value.end.nil? || (value.end.respond_to?(:infinite?) && value.end.infinite?) | |
909 | - value = {gte: value.first} | |
910 | - else | |
911 | - value = {gte: value.first, (value.exclude_end? ? :lt : :lte) => value.last} | |
912 | - end | |
907 | + value = expand_range(value) | |
913 | 908 | end |
914 | 909 | |
915 | 910 | value = {in: value} if value.is_a?(Array) |
... | ... | @@ -1138,6 +1133,17 @@ module Searchkick |
1138 | 1133 | end |
1139 | 1134 | end |
1140 | 1135 | |
1136 | + def expand_range(range) | |
1137 | + expanded = {} | |
1138 | + expanded[:gte] = range.begin if range.begin | |
1139 | + | |
1140 | + if range.end && !(range.end.respond_to?(:infinite?) && range.end.infinite?) | |
1141 | + expanded[range.exclude_end? ? :lt : :lte] = range.end | |
1142 | + end | |
1143 | + | |
1144 | + expanded | |
1145 | + end | |
1146 | + | |
1141 | 1147 | def base_field(k) |
1142 | 1148 | k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end|exact)\z/, "") |
1143 | 1149 | end | ... | ... |
test/where_test.rb
... | ... | @@ -46,6 +46,11 @@ class WhereTest < Minitest::Test |
46 | 46 | # use eval to prevent parse error |
47 | 47 | assert_search "product", ["Product C", "Product D"], where: {store_id: eval("3..")} |
48 | 48 | end |
49 | + if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7.0") | |
50 | + # use eval to prevent parse error | |
51 | + assert_search "product", ["Product A", "Product B"], where: {store_id: eval("..2")} | |
52 | + assert_search "product", ["Product A", "Product B"], where: {store_id: eval("...3")} | |
53 | + end | |
49 | 54 | |
50 | 55 | # or |
51 | 56 | assert_search "product", ["Product A", "Product B", "Product C"], where: {or: [[{in_stock: true}, {store_id: 3}]]} | ... | ... |