Skip to content

Commit

Permalink
adds default date and time to new distribution form to fix 3466 (#3530)
Browse files Browse the repository at this point in the history
* adds default date and time to new distribution form to fix 3466
  • Loading branch information
cotabas authored Aug 18, 2023
1 parent d16d69d commit 46ec021
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/issued_at.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions app/views/distributions/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
collection: current_organization.partners.alphabetized,
label: "Partner",
error: "Which partner is this distribution going to?" %>

<div class='w-72'>
<%= 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")} %>
</div>
<%= 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" %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/distributions/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<!-- Default box -->
<div class="box">
<div class="box-body">
<%= render 'form', distribution: @distribution %>
<%= render 'form', distribution: @distribution, date_place_holder: @distribution.issued_at %>
</div>
</div><!-- /.box -->
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/distributions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<div class="card-body">
<!-- Default box -->
<div class="box">
<%= render 'form', distribution: @distribution %>
<%= render 'form', distribution: @distribution, date_place_holder: Time.zone.now.end_of_day %>
</div><!-- /.box -->
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions spec/models/distribution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -183,15 +183,15 @@
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

distribution = create(:distribution, created_at: yesterday, issued_at: today)
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
Expand Down
6 changes: 3 additions & 3 deletions spec/models/donation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@
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

donation = create(:donation, created_at: yesterday, issued_at: today)
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
Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions spec/models/purchase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@
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

purchase = create(:purchase, created_at: yesterday, issued_at: today)
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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion spec/requests/distributions_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 46ec021

Please sign in to comment.