diff --git a/app/models/casa_case.rb b/app/models/casa_case.rb index bc1e94a652..335f186897 100644 --- a/app/models/casa_case.rb +++ b/app/models/casa_case.rb @@ -158,6 +158,12 @@ def most_recent_past_court_date court_dates.where("date < ?", Date.today).order(:date).last end + def formatted_latest_court_date + most_recent = most_recent_past_court_date&.date&.in_time_zone || Time.zone.now + + most_recent.strftime(::DateHelper::RUBY_MONTH_DAY_YEAR_FORMAT) + end + def has_judge_name? judge_name end diff --git a/app/views/casa_cases/_generate_report_modal.html.erb b/app/views/casa_cases/_generate_report_modal.html.erb index 26f97f604c..b807383c38 100644 --- a/app/views/casa_cases/_generate_report_modal.html.erb +++ b/app/views/casa_cases/_generate_report_modal.html.erb @@ -22,7 +22,7 @@
<%= form.text_field :start_date, - value: Time.zone.now.strftime(::DateHelper::RUBY_MONTH_DAY_YEAR_FORMAT), + value: @casa_case.formatted_latest_court_date, data: { provide: "datepicker", date_format: ::DateHelper::JQUERY_MONTH_DAY_YEAR_FORMAT }, class: "form-control" %> diff --git a/spec/models/casa_case_spec.rb b/spec/models/casa_case_spec.rb index 4a4dbc96fc..2af5d61c9e 100644 --- a/spec/models/casa_case_spec.rb +++ b/spec/models/casa_case_spec.rb @@ -297,6 +297,34 @@ end end + describe "#formatted_latest_court_date" do + let(:casa_case) { create(:casa_case) } + + before do + travel_to Date.new(2021, 1, 1) + end + + context "with a past court date" do + it "returns the latest past court date as a formatted string" do + most_recent_past_court_date = create(:court_date, date: 3.months.ago) + + casa_case.court_dates << create(:court_date, date: 9.months.ago) + casa_case.court_dates << most_recent_past_court_date + casa_case.court_dates << create(:court_date, date: 15.months.ago) + + expect(casa_case.formatted_latest_court_date).to eq("October 01, 2020") # 3 months before 1/1/21 + end + end + + context "without a past court date" do + it "returns the current day as a formatted string" do + allow(casa_case).to receive(:most_recent_past_court_date).and_return(nil) + + expect(casa_case.formatted_latest_court_date).to eq("January 01, 2021") + end + end + end + context "#remove_emancipation_category" do let(:casa_case) { create(:casa_case) } let(:emancipation_category) { build(:emancipation_category) }