From d722bdfa46afd9c73de32f820a9cff09769a18fb Mon Sep 17 00:00:00 2001 From: Spencer Markowski Date: Wed, 20 Mar 2024 08:15:53 -0400 Subject: [PATCH] Inclusive ranges adjust the upper bound condition Fixes #91 --- lib/stretchy/shared_scopes.rb | 7 ++++++- spec/stretchy/querying_spec.rb | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/stretchy/shared_scopes.rb b/lib/stretchy/shared_scopes.rb index cdda7ac..fdb4b1c 100644 --- a/lib/stretchy/shared_scopes.rb +++ b/lib/stretchy/shared_scopes.rb @@ -4,7 +4,12 @@ module SharedScopes included do - scope :between, ->(range, range_field = "created_at") { filter_query(:range, range_field => {gte: range.begin, lte: range.end}) } + scope :between, ->(range, range_field = "created_at") do + range_options = {gte: range.begin} + upper_bound = range.exclude_end? ? :lt : :lte + range_options[upper_bound] = range.end + filter_query(:range, range_field => range_options) + end scope :using_time_based_indices, lambda { |range| search_options(index: time_based_indices(range)) } end diff --git a/spec/stretchy/querying_spec.rb b/spec/stretchy/querying_spec.rb index 0dfd8a4..2d20753 100644 --- a/spec/stretchy/querying_spec.rb +++ b/spec/stretchy/querying_spec.rb @@ -99,10 +99,16 @@ end context 'when using ranges' do - it 'handles date ranges' do + it 'gte and lte with .. ranges' do begin_date = 2.days.ago.beginning_of_day.utc end_date = 1.day.ago.end_of_day.utc - expect(described_class.where(date: begin_date...end_date).to_elastic[:query][:bool]).to eq({filter: [{range: {date: {gte: begin_date, lte: end_date}}}]}.with_indifferent_access) + expect(described_class.where(date: begin_date..end_date).to_elastic[:query][:bool]).to eq({filter: [{range: {date: {gte: begin_date, lte: end_date}}}]}.with_indifferent_access) + end + + it 'gte and lt with ... ranges' do + begin_date = 2.days.ago.beginning_of_day.utc + end_date = 1.day.ago.end_of_day.utc + expect(described_class.where(date: begin_date...end_date).to_elastic[:query][:bool]).to eq({filter: [{range: {date: {gte: begin_date, lt: end_date}}}]}.with_indifferent_access) end it 'handles integer ranges' do