From 0dc9a7b01dfd6ac2d9b5c1c44767a38b64c74a72 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 13 Apr 2022 14:02:19 +0100 Subject: [PATCH] Fix saving date --- app/models/case_log.rb | 2 +- spec/models/case_log_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index f3f7648f0..1171ec694 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -41,7 +41,7 @@ class CaseLog < ApplicationRecord years.each { |year| query = query.or(filter_by_year(year)) } query.all } - scope :filter_by_year, ->(year) { where(startdate: Time.utc(year.to_i, 4, 1)...Time.utc(year.to_i + 1, 4, 1)) } + scope :filter_by_year, ->(year) { where(startdate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } scope :filter_by_user, lambda { |selected_user, user| if !selected_user.include?("all") && user.present? diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 8a04fc1e9..3f10f92bd 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1846,6 +1846,14 @@ RSpec.describe CaseLog do it "can filter by year(s) AND status" do expect(described_class.filter_by_years(%w[2021 2022]).filter_by_status("completed").count).to eq(1) end + + it "filters based on date boundaries correctly" do + case_log_1.update!(startdate: Time.zone.local(2022, 4, 1)) + case_log_2.update!(startdate: Time.zone.local(2022, 3, 31)) + + expect(described_class.filter_by_years(%w[2021]).count).to eq(1) + expect(described_class.filter_by_years(%w[2022]).count).to eq(2) + end end context "when filtering on status" do