From ee122006a07a4ba0bd741ed8c87801a74edb8362 Mon Sep 17 00:00:00 2001 From: adamlazik1 Date: Tue, 14 Jan 2025 16:21:38 +0100 Subject: [PATCH] Improve search queries based on datetime This is heavily work in progresss and the content will most likely change. --- lib/scoped_search/query_builder.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/scoped_search/query_builder.rb b/lib/scoped_search/query_builder.rb index eae5fdb..710bf4e 100644 --- a/lib/scoped_search/query_builder.rb +++ b/lib/scoped_search/query_builder.rb @@ -146,8 +146,16 @@ def datetime_test(field, operator, value, &block) # :yields: finder_option_type, # but the field is of datetime type. Change the comparison to return # more logical results. if field.datetime? - span = 1.minute if(value =~ /\A\s*\d+\s+\bminutes?\b\s+\bago\b\s*\z/i) - span ||= (timestamp.day_fraction == 0) ? 1.day : 1.hour + if value =~ /(\d{1,2}:){2}\d{1,2}/ + span = 1.second + elsif value =~ /(\A\s*\d+\s+\bminutes?\b\s+\bago\b\s*\z|\d{1,2}:\d{1,2})/i + span = 1.minute + elsif timestamp.day_fraction != 0 + span = 1.hour + else + span = 1.day + end + if [:eq, :ne].include?(operator) # Instead of looking for an exact (non-)match, look for dates that # fall inside/outside the range of timestamps of that day. @@ -155,13 +163,13 @@ def datetime_test(field, operator, value, &block) # :yields: finder_option_type, field_sql = field.to_sql(operator, &block) return ["#{negate}(#{field_sql} >= ? AND #{field_sql} < ?)", timestamp, timestamp + span] - elsif operator == :gt + elsif span == 1.day && operator == :gt # Make sure timestamps on the given date are not included in the results # by moving the date to the next day. timestamp += span operator = :gte - elsif operator == :lte + elsif span == 1.day && operator == :lte # Make sure the timestamps of the given date are included by moving the # date to the next date. timestamp += span