From 46ec0210876e0496198f394b78ac5e50d6f70cc7 Mon Sep 17 00:00:00 2001 From: Morgan Hufford Date: Fri, 18 Aug 2023 09:04:09 -0700 Subject: [PATCH] adds default date and time to new distribution form to fix 3466 (#3530) * adds default date and time to new distribution form to fix 3466 --- app/controllers/distributions_controller.rb | 2 +- app/models/concerns/issued_at.rb | 2 +- app/views/distributions/_form.html.erb | 3 +-- app/views/distributions/edit.html.erb | 2 +- app/views/distributions/new.html.erb | 2 +- spec/models/distribution_spec.rb | 6 +++--- spec/models/donation_spec.rb | 6 +++--- spec/models/purchase_spec.rb | 8 ++++---- spec/requests/distributions_requests_spec.rb | 1 - 9 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/controllers/distributions_controller.rb b/app/controllers/distributions_controller.rb index 0860f43a4b..ba89b14ca3 100644 --- a/app/controllers/distributions_controller.rb +++ b/app/controllers/distributions_controller.rb @@ -102,7 +102,7 @@ def create format.turbo_stream do flash.now[:error] = flash_error render turbo_stream: [ - turbo_stream.replace(@distribution, partial: "form", locals: {distribution: @distribution}), + turbo_stream.replace(@distribution, partial: "form", locals: {distribution: @distribution, date_place_holder: @distribution.issued_at}), turbo_stream.replace("flash", partial: "shared/flash") ], status: :bad_request end diff --git a/app/models/concerns/issued_at.rb b/app/models/concerns/issued_at.rb index ff2405b7a5..35045a6959 100644 --- a/app/models/concerns/issued_at.rb +++ b/app/models/concerns/issued_at.rb @@ -15,7 +15,7 @@ module IssuedAt private def initialize_issued_at - self.issued_at ||= created_at + self.issued_at ||= created_at&.end_of_day end def issued_at_cannot_be_before_2000 diff --git a/app/views/distributions/_form.html.erb b/app/views/distributions/_form.html.erb index b1c5ac2285..29875d5d12 100644 --- a/app/views/distributions/_form.html.erb +++ b/app/views/distributions/_form.html.erb @@ -9,9 +9,8 @@ collection: current_organization.partners.alphabetized, label: "Partner", error: "Which partner is this distribution going to?" %> -
- <%= f.input :issued_at, as: :datetime, ampm: true, minute_step: 15, label: "Distribution date", html5: true %> + <%= f.input :issued_at, as: :datetime, ampm: true, minute_step: 15, label: "Distribution date and time", html5: true, :input_html => { :value => date_place_holder&.strftime("%Y-%m-%dT%0k:%M")} %>
<%= f.input :reminder_email_enabled, as: :boolean, checked_value: true, unchecked_value: false, label: "Send email reminder the day before?" %> <%= f.input :agency_rep, label: "Agency representative" %> diff --git a/app/views/distributions/edit.html.erb b/app/views/distributions/edit.html.erb index 839089faf8..c22a8ef5be 100644 --- a/app/views/distributions/edit.html.erb +++ b/app/views/distributions/edit.html.erb @@ -46,7 +46,7 @@
- <%= render 'form', distribution: @distribution %> + <%= render 'form', distribution: @distribution, date_place_holder: @distribution.issued_at %>
diff --git a/app/views/distributions/new.html.erb b/app/views/distributions/new.html.erb index ea549040b0..38fa80e41a 100644 --- a/app/views/distributions/new.html.erb +++ b/app/views/distributions/new.html.erb @@ -34,7 +34,7 @@
- <%= render 'form', distribution: @distribution %> + <%= render 'form', distribution: @distribution, date_place_holder: Time.zone.now.end_of_day %>
diff --git a/spec/models/distribution_spec.rb b/spec/models/distribution_spec.rb index a4881a44d6..418619e686 100644 --- a/spec/models/distribution_spec.rb +++ b/spec/models/distribution_spec.rb @@ -99,7 +99,7 @@ create(:distribution, issued_at: Date.yesterday) # and one outside the range create(:distribution, issued_at: 1.year.ago) - expect(Distribution.during(Time.zone.now - 1.week..Time.zone.now).size).to eq(2) + expect(Distribution.during(Time.zone.now - 1.week..Time.zone.now + 2.days).size).to eq(2) end end @@ -183,7 +183,7 @@ end context "Callbacks >" do - it "initializes the issued_at field to default to created_at if it wasn't explicitly set" do + it "initializes the issued_at field to default to midnight if it wasn't explicitly set" do yesterday = 1.day.ago today = Time.zone.today @@ -191,7 +191,7 @@ expect(distribution.issued_at.to_date).to eq(today) distribution = create(:distribution, created_at: yesterday) - expect(distribution.issued_at).to eq(distribution.created_at) + expect(distribution.issued_at).to eq(distribution.created_at.end_of_day) end context "#before_save" do diff --git a/spec/models/donation_spec.rb b/spec/models/donation_spec.rb index 4151d06e04..2c5b973ff3 100644 --- a/spec/models/donation_spec.rb +++ b/spec/models/donation_spec.rb @@ -60,7 +60,7 @@ end context "Callbacks >" do - it "inititalizes the issued_at field to default to created_at if it wasn't explicitly set" do + it "inititalizes the issued_at field to default to midnight if it wasn't explicitly set" do yesterday = 1.day.ago today = Time.zone.today @@ -68,7 +68,7 @@ expect(donation.issued_at.to_date).to eq(today) donation = create(:donation, created_at: yesterday) - expect(donation.issued_at).to eq(donation.created_at) + expect(donation.issued_at).to eq(donation.created_at.end_of_day) end it "automatically combines duplicate line_item records when they're created" do @@ -92,7 +92,7 @@ create(:donation, issued_at: Date.yesterday) # and one outside the range create(:donation, issued_at: 1.year.ago) - expect(Donation.during(1.month.ago..Date.tomorrow).size).to eq(2) + expect(Donation.during(1.month.ago..Time.zone.now + 2.days).size).to eq(2) end end diff --git a/spec/models/purchase_spec.rb b/spec/models/purchase_spec.rb index 842836e716..1e17b2ed69 100644 --- a/spec/models/purchase_spec.rb +++ b/spec/models/purchase_spec.rb @@ -90,7 +90,7 @@ end context "Callbacks >" do - it "inititalizes the issued_at field to default to created_at if it wasn't explicitly set" do + it "inititalizes the issued_at field to default to midnight if it wasn't explicitly set" do yesterday = 1.day.ago today = Time.zone.today @@ -98,7 +98,7 @@ expect(purchase.issued_at.to_date).to eq(today) purchase = create(:purchase, created_at: yesterday) - expect(purchase.issued_at).to eq(purchase.created_at) + expect(purchase.issued_at).to eq(purchase.created_at.end_of_day) end it "automatically combines duplicate line_item records when they're created" do @@ -116,14 +116,14 @@ describe "during >" do it "returns all purchases created between two dates" do Purchase.destroy_all - # The models should default to assigning the created_at time to the issued_at + # The models should default to assigning midnight to the issued_at create(:purchase, created_at: Time.zone.today) # but just for fun we'll force one in the past within the range create(:purchase, issued_at: Date.yesterday) # and one outside the range create(:purchase, issued_at: 1.year.ago) - expect(Purchase.during(1.month.ago..Date.tomorrow).size).to eq(2) + expect(Purchase.during(1.month.ago..Time.zone.now + 2.days).size).to eq(2) end end end diff --git a/spec/requests/distributions_requests_spec.rb b/spec/requests/distributions_requests_spec.rb index 3fea5dccff..20294134bd 100644 --- a/spec/requests/distributions_requests_spec.rb +++ b/spec/requests/distributions_requests_spec.rb @@ -202,7 +202,6 @@ second_item = create(:item) first_distribution = create(:distribution) second_distribution = create(:distribution) - create(:line_item, :distribution, item_id: first_item.id, itemizable_id: first_distribution.id, quantity: 7) create(:line_item, :distribution, item_id: first_item.id, itemizable_id: second_distribution.id, quantity: 4) create(:line_item, :distribution, item_id: second_item.id, itemizable_id: second_distribution.id, quantity: 5)