Fix multiple day filter bug, by explicitly applying the from clause when it's present

pull/11764/head
Dombi Attila 2 years ago
parent dd46754b61
commit 64b2af75ea
  1. 13
      app/models/queries/days/day_query.rb
  2. 25
      spec/models/queries/days/day_query_spec.rb

@ -36,14 +36,13 @@ class Queries::Days::DayQuery < Queries::BaseQuery
end end
## ##
# This override is necessary, the dates interval filter needs to adjust the # The dates interval filter needs to adjust the `from` clause of the query.
# `from` clause of the query. To update the `from` clause, we reverse merge the filters, # If there are multiple filters with custom from clause (currently not possible),
# otherwise the `from` clause of the filter is ignored. # the first one is applied and the rest is ignored.
def apply_filters(scope) def apply_filters(scope)
filters.each do |filter| scope = super(scope)
scope = filter.scope.merge(scope) from_clause_filter = filters.find(&:from)
end scope = scope.from(from_clause_filter.from) if from_clause_filter
scope scope
end end

@ -60,22 +60,25 @@ describe Queries::Days::DayQuery, type: :model do
instance.where('date', '<>d', date_range) instance.where('date', '<>d', date_range)
end end
context 'with dates within the default range' do shared_examples_for 'dates within the default range' do |working: nil|
let(:from) { Time.zone.today } let(:from) { Time.zone.today }
let(:to) { 5.days.from_now.to_date } let(:to) { 5.days.from_now.to_date }
let(:base_scope) { Day.from_range(from:, to:).reorder(date: :asc) } let(:base_scope) { Day.from_range(from:, to:).reorder(date: :asc) }
it 'is the same as handwriting the query' do it 'is the same as handwriting the query' do
# Expectation has to be weirdly specific to the logic of Queries::Operators::DateRangeClauses # Expectation has to be weirdly specific to the logic of Queries::Operators::DateRangeClauses
expected = base_scope.where("days.date > ? AND days.date <= ?", expected_scope = base_scope.where("days.date > ? AND days.date <= ?",
(from - 1.day).end_of_day, (from - 1.day).end_of_day,
to.end_of_day) to.end_of_day)
expect(instance.results.to_sql).to eql expected.to_sql unless working.nil?
expected_scope = expected_scope.where("days.working IN ('#{working}')")
end
expect(instance.results.to_sql).to eql expected_scope.to_sql
end end
end end
context 'with dates out of the default range' do shared_examples_for 'dates out of the default range' do
let(:from) { 5.days.from_now.to_date } let(:from) { 5.days.from_now.to_date }
let(:to) { 153.days.from_now.to_date } let(:to) { 153.days.from_now.to_date }
@ -101,5 +104,17 @@ describe Queries::Days::DayQuery, type: :model do
end end
end end
end end
include_examples 'dates within the default range'
include_examples 'dates out of the default range'
context 'when having a working filter too' do
before do
instance.where('working', '=', 't')
end
include_examples 'dates within the default range', working: 't'
include_examples 'dates out of the default range'
end
end end
end end

Loading…
Cancel
Save