[PostVersions] Allow querying by updated_at (#762)

Resolves #753
This commit is contained in:
Donovan Daniels 2024-09-29 15:07:00 -05:00 committed by GitHub
parent 2ba1173d36
commit cffc480b23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -85,6 +85,11 @@ class PostVersion < ApplicationRecord
must = tag_list(:locked_tags_removed, params[:locked_tags_removed], must)
must = tag_list(:locked_tags_added, params[:locked_tags_added], must)
if params[:updated_at].present?
updated = range_relation(ParseValue.range(params[:updated_at], :date), :updated_at)
must << updated if updated.present?
end
if params[:reason].present?
must << { match: { reason: params[:reason] } }
end
@ -116,6 +121,33 @@ class PostVersion < ApplicationRecord
}
end
def range_relation(arr, field)
return if arr.nil?
return if arr.size < 2
return if arr[1].nil?
case arr[0]
when :eq
if arr[1].is_a?(Time)
{ range: { field => { gte: arr[1].beginning_of_day, lte: arr[1].end_of_day } } }
else
{ term: { field => arr[1] } }
end
when :gt
{ range: { field => { gt: arr[1] } } }
when :gte
{ range: { field => { gte: arr[1] } } }
when :lt
{ range: { field => { lt: arr[1] } } }
when :lte
{ range: { field => { lte: arr[1] } } }
when :in
{ terms: { field => arr[1] } }
when :between
{ range: { field => { gte: arr[1], lte: arr[2] } } }
end
end
def boolean_match(attribute, value, must)
if value&.truthy?
must << { term: { attribute => true } }