Skip to content

Commit 9811a97

Browse files
authored
Fix unexpected issued_at behavior when date field empty. (#4849)
* Fix unexpected issued_at behavior when date field empty. Previously before_save and before_create callbacks set the value for issued_at created_at end of day if no datetime was provided. This prevented records being saved with no issue date. But it caused unexpected behavior when creating or updating a donation/purchase/distribution with an empty issued_at field, silently changing the issued_at date to something potentially incorrect. * Fix missing partner dropdown options of distribution update fail * Revert partner list fix
1 parent 8373b0f commit 9811a97

18 files changed

+212
-54
lines changed

app/models/concerns/issued_at.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@ module IssuedAt
55
extend ActiveSupport::Concern
66

77
included do
8-
before_create :initialize_issued_at
9-
before_save :initialize_issued_at
108
scope :by_issued_at, ->(issued_at) { where(issued_at: issued_at.beginning_of_month..issued_at.end_of_month) }
119
scope :for_year, ->(year) { where("extract(year from issued_at) = ?", year) }
10+
validates :issued_at, presence: true
1211
validate :issued_at_cannot_be_before_2000
1312
validate :issued_at_cannot_be_further_than_1_year
1413
end
1514

1615
private
1716

18-
def initialize_issued_at
19-
self.issued_at ||= created_at&.end_of_day
20-
end
21-
2217
def issued_at_cannot_be_before_2000
2318
if issued_at.present? && issued_at < Date.new(2000, 1, 1)
24-
errors.add(:issued_at, "Cannot be before 2000")
19+
errors.add(:issued_at, "cannot be before 2000")
2520
end
2621
end
2722

app/views/distributions/edit.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
<section class="content">
2626

27-
<% unless @distribution.future? %>
27+
<% if @distribution.issued_at && !@distribution.future? %>
2828
<div class="alert alert-warning" role="alert">
2929
The current date is past the date this distribution was scheduled for.
3030
Please be very careful when editing this record;

config/locales/en.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@
2929
# To learn more, please read the Rails Internationalization guide
3030
# available at http://guides.rubyonrails.org/i18n.html.
3131

32-
en:
32+
en:
33+
activerecord:
34+
attributes:
35+
donation:
36+
issued_at: "Issue date"
37+
purchase:
38+
issued_at: "Purchase date"
39+
distribution:
40+
issued_at: "Distribution date and time"

spec/controllers/distributions_controller_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
organization_name: organization.id,
1919
distribution: {
2020
partner_id: partner.id,
21+
issued_at: Date.yesterday,
2122
storage_location_id: first_storage_location.id,
2223
line_items_attributes:
2324
{
@@ -42,6 +43,7 @@
4243
distribution: {
4344
partner_id: partner.id,
4445
storage_location_id: second_storage_location.id,
46+
issued_at: Date.yesterday,
4547
line_items_attributes:
4648
{
4749
"0": { item_id: second_storage_location.items.first.id, quantity: 18 }
@@ -66,6 +68,7 @@
6668
distribution: {
6769
partner_id: partner.id,
6870
storage_location_id: storage_location.id,
71+
issued_at: Date.yesterday,
6972
line_items_attributes:
7073
{
7174
"0": { item_id: storage_location.items.first.id, quantity: 18 }
@@ -91,6 +94,7 @@
9194
distribution: {
9295
partner_id: partner.id,
9396
storage_location_id: storage_location.id,
97+
issued_at: Date.yesterday,
9498
line_items_attributes:
9599
{
96100
"0": { item_id: storage_location.items.first.id, quantity: 18 },
@@ -134,6 +138,7 @@
134138
distribution: {
135139
partner_id: partner.id,
136140
storage_location_id: storage_location.id,
141+
issued_at: Date.yesterday,
137142
line_items_attributes:
138143
{
139144
"0": { item_id: item1.id, quantity: 18 },
@@ -172,6 +177,7 @@
172177
distribution: {
173178
partner_id: partner.id,
174179
storage_location_id: storage_location.id,
180+
issued_at: Date.yesterday,
175181
line_items_attributes:
176182
{
177183
"0": { item_id: item1.id, quantity: 18 },
@@ -199,6 +205,7 @@
199205
distribution: {
200206
partner_id: partner.id,
201207
storage_location_id: storage_location.id,
208+
issued_at: Date.yesterday,
202209
line_items_attributes:
203210
{
204211
"0": { item_id: storage_location.items.first.id, quantity: 0 }
@@ -235,6 +242,7 @@
235242
id: distribution.id,
236243
distribution: {
237244
storage_location_id: distribution.storage_location.id,
245+
issued_at: Date.yesterday,
238246
line_items_attributes:
239247
{
240248
"0": { item_id: item1.id, quantity: 18 },
@@ -309,6 +317,7 @@
309317
id: distribution.id,
310318
distribution: {
311319
storage_location_id: distribution.storage_location.id,
320+
issued_at: Date.yesterday,
312321
line_items_attributes:
313322
{
314323
"0": { item_id: item1.id, quantity: 4 },

spec/controllers/donations_controller_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
donation: { storage_location_id: storage_location.id,
3535
donation_site_id: donation_site.id,
3636
source: "Donation Site",
37+
issued_at: Date.yesterday,
3738
line_items: line_items }
3839
}
3940
expect(response).to redirect_to(donations_path)

spec/factories/distributions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
storage_location
2323
partner
2424
organization { Organization.try(:first) || create(:organization) }
25-
issued_at { nil }
25+
issued_at { Time.current }
2626
delivery_method { :pick_up }
2727
state { :scheduled }
2828

spec/factories/donations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
comment { "It's a fine day for diapers." }
2424
storage_location
2525
organization { Organization.try(:first) || create(:organization) }
26-
issued_at { nil }
26+
issued_at { Time.current }
2727

2828
factory :manufacturer_donation do
2929
manufacturer

spec/factories/purchases.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
purchased_from { "Google" }
2525
storage_location
2626
organization { Organization.try(:first) || create(:organization) }
27-
issued_at { nil }
27+
issued_at { Time.current }
2828
amount_spent_in_cents { 10_00 }
2929
vendor { Vendor.try(:first) || create(:vendor) }
3030

spec/models/distribution_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,6 @@
240240
end
241241

242242
context "Callbacks >" do
243-
it "initializes the issued_at field to default to midnight if it wasn't explicitly set" do
244-
yesterday = 1.day.ago
245-
today = Time.zone.today
246-
247-
distribution = create(:distribution, created_at: yesterday, issued_at: today)
248-
expect(distribution.issued_at.to_date).to eq(today)
249-
250-
distribution = create(:distribution, created_at: yesterday)
251-
expect(distribution.issued_at).to eq(distribution.created_at.end_of_day)
252-
end
253-
254243
context "#before_save" do
255244
context "#reset_shipping_cost" do
256245
context "when delivery_method is other then shipped" do

spec/models/donation_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,6 @@
6161
end
6262

6363
context "Callbacks >" do
64-
it "inititalizes the issued_at field to default to midnight if it wasn't explicitly set" do
65-
yesterday = 1.day.ago
66-
today = Time.zone.today
67-
68-
donation = create(:donation, created_at: yesterday, issued_at: today)
69-
expect(donation.issued_at.to_date).to eq(today)
70-
71-
donation = create(:donation, created_at: yesterday)
72-
expect(donation.issued_at).to eq(donation.created_at.end_of_day)
73-
end
74-
7564
it "automatically combines duplicate line_item records when they're created" do
7665
donation = build(:donation)
7766
item = create(:item)

0 commit comments

Comments
 (0)